2014-02-17 67 views
1

的分配很簡單,我們需要做的是代碼創建一個窗口有一個紅色的面板採用了單獨的按鈕和標籤。這是迄今爲止的代碼以及測試器類。 我把標籤顯示在窗口上,但它在一個奇怪的地方。我不能讓按鈕顯示,並讓背景顯示爲紅色。嘗試使用GUI,有麻煩添加按鈕和標籤

這是我遇到的最麻煩的:

import java.awt.Color; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class MyCustomFrame extends JFrame 
{ 
    public MyCustomFrame() 
    { 
     createComponents(); 
     setSize(FRAME_WIDTH, FRAME_HEIGHT); 

    } 

private void createComponents() 
    { 
     JPanel panel=new createPanel(); 
     button=new JButton("Push Me"); 
     label=new JLabel("This is a label"); 
     add(button); 
     add(label); 
    } 

private JButton button; 
private JLabel label; 
final int FRAME_WIDTH = 800; 
final int FRAME_HEIGHT = 800; 

public void createPanel() 
    { 
     JPanel panel=new JPanel(); 
     panel.setBackground(Color.RED); 
     //button=new JButton("Push Me"); 
     //label=new JLabel("This is a label"); 

    } 
public void createFrame() 
    { 
     JFrame frame=new JFrame(); 
     add(frame); 
     frame.setVisible(true); 
    } 

} 

這是測試類:

import javax.swing.JFrame; 

public class MyCustomFrameViewer 
{ 
    public static void main(String[] args) 
    { 
     MyCustomFrame frame = new MyCustomFrame(); 
     frame.setTitle("My first frame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

回答

4

面板添加到框架,而不是標籤和按鈕,並將標籤和按鈕添加到面板上...

private void createComponents() 
{ 
    JPanel panel=new createPanel(); 
    add(panel); 
    button=new JButton("Push Me"); 
    label=new JLabel("This is a label"); 
    panel.add(button); 
    panel.add(label); 
} 

您可能還想看看Initial Threads,並確保您從事件的調度線程的上下文中創建你的UI

一些旁註...

這讓我害怕......

public void createFrame() 
{ 
    JFrame frame=new JFrame(); 
    add(frame); 
    frame.setVisible(true); 
} 

你試圖添加一個框架到一個框架,這是一個在Swing中的非法操作,但是幸運的是,你實際上並沒有從我看到的內容中調用它。

而不是使用setSize(FRAME_WIDTH, FRAME_HEIGHT);的,你不妨用pack代替。

+0

這個工作,非常感謝你! –

5

您創建的JPanel,面板,但沒有添加到它,然後永遠面板添加到您的JFrame。您應該將組件添加到JPanel面板,然後將面板對象添加到JFrame中。是一個JPanel使用的FlowLayout默認情況下,因此它會更容易接受無特殊方法添加多個其他組件

注意。 JFrame的contentPane使用稍微更復雜的BorderLayout。

2

參見注釋中提示在這個例子:

enter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

// Don't extend frame, just use an instance 
//public class MyCustomFrame extends JFrame { 
public class MyCustomFrame { 

    private JFrame frame; 
    private JButton button; 
    private JLabel label; 
    private JPanel panel; 
    // better to override the preferred size of the component of interest 
    final int GAME_WIDTH = 300; 
    final int GAME_HEIGHT = 100; 

    public MyCustomFrame() { 
     createComponents(); 
     // better to override the preferred size of the component of interest 
     //setSize(GAME_WIDTH, GAME_HEIGHT); 
    } 

    private void createComponents() { 
     // compilation error! createPanel() does not return anything.. 
     //JPanel panel = new createPanel(); 
     createPanel(); 
     // create the frame! 
     createFrame(); 
    } 

    private void createPanel() { 
     // creates a local instance 
     //JPanel panel = new JPanel(); 
     panel = new JPanel() { 

      /* override the preferred size of the component of interest */ 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(GAME_WIDTH, GAME_HEIGHT); 
      } 
     }; 
     panel.setBackground(Color.RED); 
     button = new JButton("Push Me"); 
     label = new JLabel("This is a label"); 
     panel.add(button); 
     panel.add(label); 
    } 

    private void createFrame() { 
     // create a local instance of a JFrame that goes out of scope at end 
     // of method.. 
     //JFrame frame = new JFrame(); 
     frame = new JFrame("My first frame"); 
     // add the panel to the frame! 
     frame.add(panel); 
     // better to use DISPOSE_ON_CLOSE 
     //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     // a good way to position a GUI 
     frame.setLocationByPlatform(true); 
     // this was trying to add a JFrame to another JFrame 
     //add(frame); 
     frame.pack(); 
    } 

    public final JFrame getFrame() { 
     return frame; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       MyCustomFrame myFrame = new MyCustomFrame(); 
       JFrame frame = myFrame.getFrame(); 
       frame.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency 
     SwingUtilities.invokeLater(r); 
    } 
}