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

很好的例子的问题可能与平台有关,但我可以提供一些观察:您没有添加或删除组件,所以您不需要revalidate().

由于背景色是按钮的绑定属性,因此不需要后续调用repaint().

你,你们做需要repaint()在你的习惯里DrawingArea,但您可能需要尝试添加属性更改支持,如建议的那样。这里.

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

下面的变化使用Queue以简化更改颜色。

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);

}}

要实现点击按钮事件弹子窗口,可以按照以下步骤进行: 1. 在Java中使用Swing库创建一个主窗口。 2. 在主窗口中添加一个按钮,并为其注册事件监听器。 3. 在事件监听器中实现点击按钮子窗口的逻辑。 4. 在逻辑中创建一个子窗口,并设置其属性和布局。 5. 将子窗口添加到主窗口中,并显示来。 以下是一个简单的示例代码: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainFrame extends JFrame implements ActionListener { private JButton btnOpen; private JDialog dlg; public MainFrame() { // 创建主窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); // 添加按钮 btnOpen = new JButton("打开子窗口"); btnOpen.addActionListener(this); getContentPane().add(btnOpen, BorderLayout.CENTER); // 显示主窗口 setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // 创建子窗口 dlg = new JDialog(this, "子窗口", true); dlg.setSize(200, 100); dlg.setLocationRelativeTo(null); // 添加标签 JLabel label = new JLabel("这是子窗口"); dlg.getContentPane().add(label, BorderLayout.CENTER); // 显示子窗口 dlg.setVisible(true); } public static void main(String[] args) { new MainFrame(); } } ``` 在上面的代码中,我们创建了一个继承自JFrame的主窗口,并添加了一个按钮。当按钮被点击,会触发我们实现的ActionListener接口的actionPerformed方法,在该方法中创建一个JDialog子窗口,并显示来。最后,我们将主窗口显示来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值