日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java按钮改变窗口大小_布局似乎有问题,JButton在调整窗口大小时显示出意外的行为。...

發(fā)布時(shí)間:2023/12/2 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java按钮改变窗口大小_布局似乎有问题,JButton在调整窗口大小时显示出意外的行为。... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

很好的例子的問題可能與平臺(tái)有關(guān),但我可以提供一些觀察:您沒有添加或刪除組件,所以您不需要revalidate().

由于背景色是按鈕的綁定屬性,因此不需要后續(xù)調(diào)用repaint().

你,你們做需要repaint()在你的習(xí)慣里DrawingArea,但您可能需要嘗試添加屬性更改支持,如建議的那樣。這里.

Color.white不可能brighter()和Color.black不可能darker();?Color.darkGray.darker()是Color.black().

下面的變化使用Queue以簡(jiǎn)化更改顏色。

import?java.awt.BorderLayout;import?java.awt.Color;import?java.awt.Dimension;import?java.awt.Graphics;

import?java.awt.GridLayout;import?java.awt.event.ActionEvent;import?java.awt.event.ActionListener;

import?java.awt.event.ComponentAdapter;import?java.awt.event.ComponentEvent;import?java.util.Arrays;

import?java.util.LinkedList;import?java.util.Queue;import?javax.swing.BorderFactory;import?javax.swing.JButton;

import?javax.swing.JComponent;import?javax.swing.JFrame;import?javax.swing.JPanel;import?javax.swing.SwingUtilities;

import?javax.swing.Timer;/**?@see?https://stackoverflow.com/q/9849950/230513?*/public?class?BallAnimation?{

private?int?x;

private?int?y;

private?boolean?positiveX;

private?boolean?positiveY;

private?boolean?isTimerRunning;

private?int?speedValue;

private?int?diameter;

private?DrawingArea?drawingArea;

private?Timer?timer;

private?Queue?clut?=?new?LinkedList(Arrays.asList(

Color.BLUE.darker(),

Color.MAGENTA.darker(),

Color.BLACK,

Color.RED.darker(),

Color.PINK,

Color.CYAN.darker(),

Color.DARK_GRAY,

Color.YELLOW.darker(),

Color.GREEN.darker()));

private?Color?backgroundColour;

private?Color?foregroundColour;

private?ActionListener?timerAction?=?new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?ae)?{

x?=?getX();

y?=?getY();

drawingArea.setXYColourValues(x,?y,?backgroundColour,?foregroundColour);

}

};

private?JPanel?buttonPanel;

private?JButton?startStopButton;

private?JButton?speedIncButton;

private?JButton?speedDecButton;

private?JButton?resetButton;

private?JButton?colourButton;

private?JButton?exitButton;

private?ComponentAdapter?componentAdapter?=?new?ComponentAdapter()?{

@Override

public?void?componentResized(ComponentEvent?ce)?{

timer.restart();

startStopButton.setText("Stop");

isTimerRunning?=?true;

}

};

public?BallAnimation()?{

x?=?y?=?0;

positiveX?=?positiveY?=?true;

speedValue?=?1;

isTimerRunning?=?false;

diameter?=?50;

backgroundColour?=?Color.white;

foregroundColour?=?clut.peek();

timer?=?new?Timer(10,?timerAction);

}

private?void?createAndDisplayGUI()?{

JFrame?frame?=?new?JFrame("Ball?Animation");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationByPlatform(true);

drawingArea?=?new?DrawingArea(x,?y,?backgroundColour,?foregroundColour,?diameter);

drawingArea.addComponentListener(componentAdapter);

frame.add(makeButtonPanel(),?BorderLayout.LINE_END);

frame.add(drawingArea,?BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

private?JPanel?makeButtonPanel()?{

buttonPanel?=?new?JPanel(new?GridLayout(0,?1));

buttonPanel.setBorder(BorderFactory.createLineBorder(Color.darkGray,?5));

startStopButton?=?new?JButton("Start");

startStopButton.setOpaque(true);

startStopButton.setForeground(Color.white);

startStopButton.setBackground(Color.green.darker());

startStopButton.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?ae)?{

if?(!isTimerRunning)?{

startStopButton.setText("Stop");

timer.start();

isTimerRunning?=?true;

}?else?if?(isTimerRunning)?{

startStopButton.setText("Start");

timer.stop();

isTimerRunning?=?false;

}

}

});

startStopButton.setBorder(BorderFactory.createLineBorder(Color.gray,?4));

buttonPanel.add(startStopButton);

colourButton?=?new?JButton("Change?Color");

colourButton.setOpaque(true);

colourButton.setForeground(Color.white);

colourButton.setBackground(clut.peek());

colourButton.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?ae)?{

//timer.restart();

clut.add(clut.remove());

foregroundColour?=?clut.peek();

drawingArea.setXYColourValues(x,?y,?backgroundColour,?foregroundColour);

colourButton.setBackground(foregroundColour);

}

});

colourButton.setBorder(BorderFactory.createLineBorder(Color.gray,?4));

buttonPanel.add(colourButton);

exitButton?=?new?JButton("Exit");

exitButton.setBackground(Color.red);

exitButton.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?ae)?{

timer.stop();

System.exit(0);

}

});

exitButton.setBorder(BorderFactory.createLineBorder(Color.red.darker(),?4));

buttonPanel.add(exitButton);

return?buttonPanel;

}

private?int?getX()?{

if?(x?

positiveX?=?true;

}?else?if?(x?>=?drawingArea.getWidth()?-?diameter)?{

positiveX?=?false;

}

return?(calculateX());

}

private?int?calculateX()?{

if?(positiveX)?{

return?(x?+=?speedValue);

}?else?{

return?(x?-=?speedValue);

}

}

private?int?getY()?{

if?(y?

positiveY?=?true;

}?else?if?(y?>=?drawingArea.getHeight()?-?diameter)?{

positiveY?=?false;

}

return?(calculateY());

}

private?int?calculateY()?{

if?(positiveY)?{

return?(y?+=?speedValue);

}?else?{

return?(y?-=?speedValue);

}

}

public?static?void?main(String...?args)?{

Runnable?runnable?=?new?Runnable()?{

@Override

public?void?run()?{

new?BallAnimation().createAndDisplayGUI();

}

};

SwingUtilities.invokeLater(runnable);

}}class?DrawingArea?extends?JComponent?{

private?int?x;

private?int?y;

private?int?ballDiameter;

private?Color?backgroundColor;

private?Color?foregroundColor;

public?DrawingArea(int?x,?int?y,?Color?bColor,?Color?fColor,?int?dia)?{

this.x?=?x;

this.y?=?y;

ballDiameter?=?dia;

backgroundColor?=?bColor;

foregroundColor?=?fColor;

setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,?5));

}

public?void?setXYColourValues(int?x,?int?y,?Color?bColor,?Color?fColor)?{

this.x?=?x;

this.y?=?y;

backgroundColor?=?bColor;

foregroundColor?=?fColor;

repaint();

}

@Override

public?Dimension?getPreferredSize()?{

return?(new?Dimension(500,?400));

}

@Override

public?void?paintComponent(Graphics?g)?{

g.setColor(backgroundColor);

g.fillRect(0,?0,?getWidth(),?getHeight());

g.setColor(foregroundColor);

g.fillOval(x,?y,?ballDiameter,?ballDiameter);

}}

總結(jié)

以上是生活随笔為你收集整理的java按钮改变窗口大小_布局似乎有问题,JButton在调整窗口大小时显示出意外的行为。...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。