0
我正在學習java GUI,我正在開發一個非常簡單的程序。Java GUI程序打開一個新的JPanel而不是替換當前面板
我有4個班,一個初始化圖形界面,其中有一定的隨機方法,和去年2相關的圖形用戶界面,這是我很感興趣的是那些因此,這裏是我的GUI類:
public class GUI extends JFrame{
Container container;
CardLayout cl;
GridBagLayout gl;
GridBagConstraints gbc;
public GUI(){
super("Game Finder");
cl = new CardLayout();
gl = new GridBagLayout();
gbc = new GridBagConstraints();
setLayout(cl);
container = this.getContentPane();
//File>New
JPanel newPanel = new JPanel();
newPanel.setLayout(gl);
newPanel.setBackground(Color.YELLOW);
JLabel newLabel = new JLabel("Time to find the game!");
JButton newButton = new JButton("Begin!");
newButton.setPreferredSize(new Dimension(200,75));
gbc.weightx=1;
gbc.weighty=1;
gbc.gridx=0;
gbc.gridy=1;
gbc.anchor = GridBagConstraints.PAGE_START;
gl.setConstraints(newButton, gbc);
newPanel.add(newButton, gbc);
newLabel.setFont(new Font("Serif", Font.BOLD, 24));
gbc.gridx=0;
gbc.gridy=0;
gbc.anchor = GridBagConstraints.CENTER;
gl.setConstraints(newLabel, gbc);
newPanel.add(newLabel, gbc);
container.add("New", newPanel);
//File>Load
JPanel loadPanel = new JPanel();
loadPanel.setBackground(Color.BLUE);
JLabel loadLabel = new JLabel("Feature yet to be implemented");
loadPanel.add(loadLabel);
container.add("Load", loadPanel);
//File>Save
JPanel savePanel = new JPanel();
savePanel.setBackground(Color.RED);
JLabel saveLabel = new JLabel("Feature yet to be implemented");
savePanel.add(saveLabel);
container.add("Save", savePanel);
//Help>About
JPanel aboutPanel = new JPanel();
JLabel aboutLabel = new JLabel();
String s = "Lorem iasd asd .";
aboutLabel.setText("<html><body style='width:230px'>" + s);
aboutPanel.setBackground(Color.WHITE);
aboutLabel.setBorder(new EmptyBorder(0,20,0,20));
aboutPanel.add(aboutLabel);
container.add("About", aboutPanel);
// Construct objects
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File"); // File menu
JMenu menuHelp = new JMenu("Help"); // Help menu
JMenuItem menuFileNew = new JMenuItem("New Game"); // New Game
JMenuItem menuFileLoad = new JMenuItem("Load Game"); // Load Game
JMenuItem menuFileSave = new JMenuItem("Save Game"); // Save Game
JMenuItem menuFileExit = new JMenuItem("Exit"); // Exit
JMenuItem menuHelpAbout = new JMenuItem("About"); // About
// Add action listener.for the menu button
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Methods().windowClosed();
}
}
);
menuFileNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "New");
}
}
);
menuFileLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "Load");
}
}
);
menuFileSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "Save");
}
}
);
menuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "About");
}
}
);
/*HERE in particular*/
newButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
GamePanels g = new GamePanels();
g.begin();
container.add("Begin", g.getPanel());
cl.show(container, "Begin");
}
});
// Add menu items to menu bar
menuFile.add(menuFileNew);
menuFile.add(menuFileLoad);
menuFile.add(menuFileSave);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuHelp.add(menuHelpAbout);
menuBar.add(menuHelp);
// Create window
setJMenuBar(menuBar);
setSize(new Dimension(350, 350));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Container getContainer(){
return this.container;
}
這裏是我的GamePanels類:
public class GamePanels {
GUI gui = new GUI();
JPanel begin;
public void begin(){
begin = new JPanel();
begin.setBackground(Color.MAGENTA);
JLabel beginLabel = new JLabel("Do you want to avoid using much of your brain?");
JButton beginYes = new JButton("Yes");
JButton beginNo = new JButton("No");
begin.add(beginLabel);
begin.add(beginYes);
begin.add(beginNo);
}
對於GUI類中的聽衆,如果我點擊像文件>有關,它完美的作品即內容與所選擇的容器面板切換。然而,當我嘗試使用「外部」面板,它開闢了一個全新的GUI界面,所以我當時有兩個這樣結束了:
假設有什麼不對我的執行?
我覺得你在那裏的東西,有道理!也許如果我看看我的主類,它是我初始化GUI的地方,如果我再次使用那個gui或者返回gui:'public static void .... {GUI initialize = new GUI(); '那麼也許我可以使用一種方法來返回現有的gui並使用那個呢? – gudthing
在這種情況下,你可以將它設置爲靜態或將它發送到你需要的參數或使用上面的組件(如果你只想使用一個幀) – Tony
已解決,我不知道爲什麼我有'GUI gui = GamePanels類中的新GUI。它實際上一直在工作,但它正在創建一個新的類實例。無論如何謝謝託尼,你讓我看得更清楚! – gudthing