所描述的问题涉及 Java 应用程序图形用户界面 (GUI) 的意外行为当调整窗口大小时。调整窗口大小会导致某些 JButton 组件的行为发生变化,包括按钮的文本和颜色。
要理解问题,必须检查代码并了解识别窗口大小和 JButton 组件行为之间的任何依赖关系。问题可能在于组件的布局方式或其属性的管理方式。
提供的代码使用 ComponentAdapter 来检测组件大小的变化绘图区域组件。当尺寸改变时,会触发以下操作:
这些操作无疑是为了确保计时器启动当调整窗口大小时。但是,该代码不处理其他组件(例如 JButton 组件)的大小调整。
意外行为可能是由 JButton 组件的布局方式引起的以及他们的财产是如何管理的。调整窗口大小时,布局管理器可能会重新排列组件,这可能会影响它们的属性。例如,按钮上的文本可能会被截断或颜色可能会改变。
要解决此问题,请考虑以下建议:
这是一个示例实现,使用 GridBagLayout 来管理布局并确保 JButton 组件保留其所需的属性:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; 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<Color> 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; 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); drawingArea = new DrawingArea(x, y, backgroundColour, foregroundColour, diameter); drawingArea.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent ce) { timer.restart(); startStopButton.setText("Stop"); isTimerRunning = true; } }); JPanel contentPane = frame.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(makeButtonPanel(), BorderLayout.EAST); contentPane.add(drawingArea, BorderLayout.CENTER); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } private JPanel makeButtonPanel() { buttonPanel = new JPanel(new GridBagLayout()); buttonPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5)); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; startStopButton = makeButton("Start", gbc, 0, 0); colourButton = makeButton("Change Color", gbc, 0, 1); exitButton = makeButton("Exit", gbc, 0, 2); return buttonPanel; } private JButton makeButton(String text, GridBagConstraints gbc, int row, int column) { JButton button = new JButton(text); button.setOpaque(true); button.setForeground(Color.WHITE); button.setBackground(Color.GREEN.DARKER); button.setBorder(BorderFactory.createLineBorder(Color.GRAY, 4)); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (button == startStopButton) { if (!isTimerRunning) { startStopButton.setText("Stop"); timer.start(); isTimerRunning = true; } else { startStopButton.setText("Start"); timer.stop(); isTimerRunning = false; } } else if (button == colourButton) { clut.add(clut.remove()); foregroundColour = clut.peek(); drawingArea.setXYColourValues(x, y, backgroundColour, foregroundColour); colourButton.setBackground(foregroundColour); } else if (button == exitButton) { timer.stop(); System.exit(0); } } }); gbc.gridx = column; gbc.gridy = row; buttonPanel.add(button, gbc); return button; } private int getX() { if (x < 0) { 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 < 0) { 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) { SwingUtilities.invokeLater(() -> new BallAnimation().createAndDisplayGUI()); } } 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) {
以上是当我调整 Java 窗口大小时,为什么我的 JButton 表现异常?的详细内容。更多信息请关注PHP中文网其他相关文章!