2014-09-06 46 views
0

使用Swing时,组件不会显示在我的JFrame中。 其实我的宗旨是:如何使用Swing在JFrame中添加面板

  1. 添加边框
  2. 在帧面板中添加
  3. 面板cantains 3个按钮

但它并没有显示出来。

这是我的代码

public class Panels 
{ 
    JFrame frame; 
    JPanel panel; 
    private JButton addButton; 
    private JButton modifyButton; 
    private JButton deleteButton; 

    Panels() 
    { 
     initGUI(); 
     launchFrame(); 
    } 

    public void initGUI() 
    { 
     frame = new JFrame(); 
     panel = new JPanel(); 
     addButton = new JButton("Add"); 
     modifyButton = new JButton("Modify"); 
     deleteButton = new JButton("Delete"); 
    } 

    public void launchFrame() 
    { 
     addButton.setBounds(130,50,225,25); 
     addButton.setBounds(150,50,225,25); 
     addButton.setBounds(170,50,225,25); 
     addButton.setBounds(190,50,225,25); 
     panel.add(addButton); 
     panel.add(modifyButton); 
     panel.add(deleteButton); 
     panel.setLayout(null); 
     panel.setBackground(Color.RED); 

     frame.add(panel); 
     frame.setTitle("My Frame with Panel"); 
     frame.setSize(600,400); 
     frame.setLocationRelativeTo(null); 
     frame.setLayout(null); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

下面是主要用于调用面板类

当在主功能帧运行被示出没有控制器(即3个按钮未示出)

public class Main 
{ 
    public static void main(String[] args) 
    { 
     Panels obj_panel=new Panels(); 
    } 
} 
+0

也许尝试在设置边界之前调用set layout null。 – Arvy 2014-09-06 08:47:16

+0

没有先生不工作 – 2014-09-06 08:48:26

+0

在这段代码中有任何问题? – 2014-09-06 08:51:50

回答

3

这是主要问题

frame.setLayout(null); 

当您将布局设置为空时,这意味着其所有组件必须设置了边界。您尝试添加面板,没有任何界限。您只需设置面板中按钮的边界。如果你删除上面的行,它的工作。

其他问题我真的看看:

  • 都不要使用空布局。相反,请使用布局管理器,并让他们处理您的大小和位置。这导致了一个更易于管理和灵活的用户界面。请花一些时间来了解不同的布局经理。开始于Laying out Components Within a Container

  • 所有Swing应用程序都应在称为事件调度线程(EDT)的特殊线程上运行。请花一些时间阅读Initial Threads以了解如何完成此操作。

这里是一个重构,没有空的布局(固定“其他问题”),只需使用布局管理器,边距和边框,并且代码在主展示了如何在运行程序事件调度线程

import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.border.EmptyBorder; 

public class Main { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       Panels obj_panel = new Panels(); 
      } 
     }); 
    } 
} 

class Panels { 

    private JFrame frame; 
    private JPanel panel; 
    private JButton addButton; 
    private JButton modifyButton; 
    private JButton deleteButton; 

    Panels() { 
     initGUI(); 
     launchFrame(); 
    } 

    private void initGUI() { 
     frame = new JFrame();  // default layout manager is BorderLayout 
     panel = new JPanel();  // default layout manager is FlowLayout 
     addButton = new JButton("Add"); 
     modifyButton = new JButton("Modify"); 
     deleteButton = new JButton("Delete"); 
    } 

    private void launchFrame() { 
     JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10)); 
     buttonPanel.setBackground(Color.RED); 
     buttonPanel.add(addButton); 
     buttonPanel.add(modifyButton); 
     // add margin to left and right of delete button 
     // other buttons will follow suit because of GridLayout 
     deleteButton.setMargin(new Insets(0, 50, 0, 50)); 
     buttonPanel.add(deleteButton); 
     // create some space at the top for the buttonPanel 
     buttonPanel.setBorder(new EmptyBorder(20, 0, 0, 0)); 

     panel.add(buttonPanel); 
     panel.setBackground(Color.RED); 

     frame.add(panel); 
     frame.setTitle("My Frame with Panel"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(600, 400); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

thankyou先生真的在工作... – 2014-09-06 15:45:39

+0

不要忘了接受(复选标记)答案,如果它有助于解决问题 – 2014-09-06 15:56:58

+0

爵士是CoreJAVA最好的gui设计制作工具吗? – 2014-09-06 16:38:25