我正在嘗試JPanel并排制作兩個按鈕。我現在有以下代碼:
JFrame frame = new JFrame("Display Shapes");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton button = new JButton();
button.setBounds(50,100,80,30);
button.setText("Load Shapes");
panel.add(button);
JButton button2 = new JButton();
button2.setBounds(100,100,80,30);
button.setText("Sort Shapes");
panel.add(button2);
frame.add(panel);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
當我運行代碼時,下面是我看到的輸出。我不確定為什么第二個按鈕沒有正確顯示。這可能是什么原因?
uj5u.com熱心網友回復:
你正在寫作的地方button.setText("Sort Shapes");,它將是button2.setText("Sort Shapes");
它正在更改先前設定文本的第一個按鈕的文本
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/323054.html
