因此,這裏是我的代碼,我仍然是Java的GUI(剛剛開始本週)的新手。據我瞭解,您可以創建一個JFrame,向其中添加JPanels來,然後在這些面板可以添加小部件就像一個JRadioButton等JFrame沒有在Java中顯示JPanel和其他小部件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Binary extends JFrame{
private JLabel header;
private JTextField userInput1;
private JButton doIt;
private JButton clear;
private JRadioButton binary, decimal;
private JLabel number2;
private JFrame frame1;
private JPanel panel1;
private JPanel panel2;
public Binary(){
super("Number Converter");
frame1 = new JFrame("Binary to Decimal Converter");
frame1.setLayout(new FlowLayout());
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(250,500);
frame1.setVisible(true);
panel1 = new JPanel();
panel1.setSize(250, 450);
frame1.add(panel1);
header = new JLabel("1- Select the mode: ");
panel1.add(header);
panel2 = new JPanel();
panel2.setSize(250, 25);
panel2.setOpaque(true);
panel2.setBackground(Color.GRAY);
frame1.add(panel2);
ButtonGroup choices= new ButtonGroup();
binary = new JRadioButton("Binary to Decimal"); // add the first radiobutton binary to decimal
choices.add(binary);
decimal = new JRadioButton("Decimal to Binary"); // add the second radiobutton decimal to binary
choices.add(decimal);
this.add(binary); // adds both to the program
this.add(decimal);
userInput1 = new JTextField(20); // Adds a blank text field for user input
frame1.add(userInput1);
number2 = new JLabel("2- Enter some words then click Do It:");
frame1.add(number2);
doIt = new JButton("Do It"); // left button do it
frame1.add(doIt);
clear = new JButton("Clear"); // right button clear
frame1.add(clear);
}
}
因爲在這裏我的代碼,當我運行它,這裏只有一幀與Binary to Decimal Converter
沒有別的,我在這裏做錯了什麼?
正如已經回答說,再加上,使用調用setVisible的'(...)'在將所有組件添加到__Top Level Container__之前,在實際確定它的大小之前,它們是罪魁禍首,它似乎是:-)嘗試刪除所有'component.setSize(...)'調用,只需將它們添加到'JFrame'一旦完成,只需調用'JFrame.pack()',然後使用'JFrame.setVisible(true)'將其設置爲可見即可。 – 2015-04-04 03:26:00