2015-04-04 63 views
1

因此,這裏是我的代碼,我仍然是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沒有別的,我在這裏做錯了什麼?

+0

正如已經回答說,再加上,使用調用setVisible的'(...)'在將所有組件添加到__Top Level Container__之前,在實際確定它的大小之前,它們是罪魁禍首,它似乎是:-)嘗試刪除所有'component.setSize(...)'調用,只需將它們添加到'JFrame'一旦完成,只需調用'JFrame.pack()',然後使用'JFrame.setVisible(true)'將其設置爲可見即可。 – 2015-04-04 03:26:00

回答

3

您正在製作JFrame的兩個副本,一個是您在屏幕上顯示的內容,另一個是要添加組件的內容。

public class Binary extends JFrame { 
    //...  
    private JFrame frame1; 
    //... 
    public Binary() { 

     super("Number Converter"); 
     frame1 = new JFrame("Binary to Decimal Converter"); 

而不是從JFrame延伸,只需創建一個實例,並使用它,而不是...

import java.awt.Color; 
import java.awt.FlowLayout; 
import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextField; 

public class Binary { 

    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() { 

     frame1 = new JFrame("Binary to Decimal Converter"); 
     frame1.setLayout(new FlowLayout()); 
     frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     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); 
     frame1.add(binary); // adds both to the program 
     frame1.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); 

     frame1.pack(); 
     frame1.setVisible(true); 
    } 

} 
+0

仍作爲空白窗口出現..:/ – X1XX 2015-04-04 03:40:57

+0

嘗試使用新的更改 – MadProgrammer 2015-04-04 03:43:42

+0

它的工作原理!但是,我如何設置不同面板的尺寸?因爲即使我有'setSize'或no,它們都以相同的方式出現,我覺得'pack()'是罪魁禍首,但我怎麼能改變它,使它看起來像我想要做的? – X1XX 2015-04-04 03:49:30