在我从下面运行代码后,除了 titleBar 之外没有显示任何组件 我需要按下 graphButton,然后再次按下要显示的面板和按钮的 calcButton。 我正在尝试调用 calcButton 的所有功能,因此它是用户运行程序时的默认界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Main {
public static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public static void main(String[] args) {
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setUndecorated(true);
f.setVisible(true);
JButton[][] Buttons = new JButton[6][4];
JPanel titleBar = new JPanel();
titleBar.setPreferredSize(new Dimension(f.getWidth(), 32));
titleBar.setBackground(Color.darkGray);
JButton exitButton = new JButton("x");
exitButton.setFocusable(false);
exitButton.setForeground(Color.white);
exitButton.setBorderPainted(false);
exitButton.addActionListener(e -> System.exit(0));
exitButton.setBackground(new Color(50,50,50));
JButton minimizeButton = new JButton("-");
minimizeButton.addActionListener(e -> f.setState(Frame.ICONIFIED));
minimizeButton.setFocusable(false);
minimizeButton.setForeground(Color.white);
minimizeButton.setBorderPainted(false);
minimizeButton.setBackground(new Color(50,50,50));
JButton calcButton=new JButton("calcbutton");
calcButton.setSize(16,16);
calcButton.setBackground(titleBar.getBackground());
calcButton.setFocusable(false);
calcButton.setForeground(Color.white);
calcButton.setBorderPainted(false);
JButton graphButton=new JButton("graphbutton");
graphButton.setSize(16,16);
graphButton.setBackground(titleBar.getBackground());
graphButton.setFocusable(false);
graphButton.setForeground(Color.white);
graphButton.setBorderPainted(false);
titleBar.setLayout(new FlowLayout(FlowLayout.LEFT));
titleBar.add(calcButton);
titleBar.add(graphButton);
titleBar.setLayout(new FlowLayout(FlowLayout.RIGHT));
titleBar.add(minimizeButton);
titleBar.add(exitButton);
f.setJMenuBar(new JMenuBar() {
{ add(titleBar); }
});
for (int i = 0; i < 6; i++)
for (int j = 0; j < 4; j++) {
Buttons[i][j] = new JButton();
Buttons[i][j].setPreferredSize(new Dimension(60, 60));
Buttons[i][j].setFocusable(false);
Buttons[i][j].setBackground(new Color(20,20,20));
Buttons[i][j].setForeground(Color.white);
Buttons[i][j].setRolloverEnabled(false);
}
JPanel ButtonPanel = new JPanel();
JPanel PrintPanel = new JPanel();
JPanel PrintPanel2 = new JPanel();
JLabel PrintLabel = new JLabel();
PrintLabel.setPreferredSize(new Dimension(240, 30));
PrintLabel.setHorizontalAlignment(SwingConstants.RIGHT);
PrintPanel.setBackground(Color.darkGray);
PrintLabel.setForeground(Color.white);
PrintPanel.setPreferredSize(new Dimension(240, 30));
PrintPanel.add(PrintLabel);
JLabel PrintLabel2 = new JLabel("");
PrintPanel2.setBackground(Color.darkGray);
PrintLabel2.setForeground(Color.lightGray);
PrintLabel2.setPreferredSize(new Dimension(240, 30));
PrintLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
PrintPanel2.add(PrintLabel2);
PrintPanel2.setPreferredSize(new Dimension(240, 30));
ButtonPanel.setLayout(new GridLayout(6, 4));
for (int i = 0; i < 6; i++)
for (int j = 0; j < 4; j++)
ButtonPanel.add(Buttons[i][j]);
Graph panel=new Graph();
GridBagLayout gridBagLayout = new GridBagLayout();
f.setSize(screenSize.width/7, screenSize.height/2);
f.setLocation(screenSize.width/2-f.getWidth()/2,screenSize.height/2-f.getHeight()/2);
f.setLayout(gridBagLayout);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
PrintPanel2.setVisible(true);
f.add(PrintPanel2, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
PrintPanel.setVisible(true);
f.add(PrintPanel, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.BOTH;
ButtonPanel.setVisible(true);
f.add(ButtonPanel, constraints);
f.revalidate();
calcButton.addMouseListener(new MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
f.remove(panel);
f.repaint();
f.setSize(screenSize.width/7, screenSize.height/2);
f.setLayout(gridBagLayout);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
f.add(PrintPanel2, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
f.add(PrintPanel, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.BOTH;
f.add(ButtonPanel, constraints);
f.setVisible(true);
f.revalidate();
}
});
graphButton.addMouseListener(new MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
f.setSize(screenSize.width/2, screenSize.height/2);
f.remove(PrintPanel);
f.remove(PrintPanel2);
f.remove(ButtonPanel);
f.repaint();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(panel, BorderLayout.CENTER);
f.setVisible(true);
f.revalidate();
}
});
}
public static class Graph extends JPanel {
public Graph() {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setBackground(Color.BLACK);
}
}
}
这是呈现相同问题的简化代码 我尝试在每个更改块后重新绘制框架,我尝试检查添加到框架中的组件数量,但所有组件都已添加,因此它们一定不可见,所以我尝试为我的所有组件调用 setVisible(true) 方法成分。看到它在按下 graphButton 然后再次按下 calcButton 后起作用,我尝试使用 doClick() 方法使其自动化,但它仍然不起作用。