2012-10-23 217 views
3

我在这里有这个代码,想法是在一个主窗口中有一个文本区域旁边有两个按钮,我还没有添加。在尝试使用GridBagLayout并在此过程中撕掉头发之后,我决定不使用布局,并在不可调整大小的窗口内手动定位按钮。按钮和窗口之间的空间

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class Tema extends JFrame implements ActionListener { 

JMenuBar menubar = new JMenuBar(); 
JMenu actiuni = new JMenu("Actiuni"); 
JMenu contact = new JMenu("Contact"); 
JMenu help = new JMenu("Help"); 
JMenuItem ntest = new JMenuItem("Nou test"); 
JMenuItem vizarh = new JMenuItem("Vizualizare arhiva"); 
JMenuItem datcon = new JMenuItem("Date de contact"); 
JMenuItem sendmail = new JMenuItem("Trimite e-mail"); 
JMenuItem instrut = new JMenuItem("Instructiuni de utilizare"); 
JButton b1 = new JButton("Incepe testul!"); 
JButton b2 = new JButton("Vezi arhiva!"); 
JTextArea ta = new JTextArea("Default text", 5, 30); 

public void common(String s) 
{ 
    setSize(800,450); 
    setLocationRelativeTo(null); 
    setResizable(false); 

    setTitle(s); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    menubar.add(actiuni); 
    menubar.add(contact); 
    menubar.add(help); 
    actiuni.add(ntest); 
    actiuni.add(vizarh); 
    contact.add(datcon); 
    contact.add(sendmail); 
    help.add(instrut); 

    setJMenuBar(menubar); 

} 

public Tema() 
{ 
    common("Self-Esteem- Fereastra Principala"); 
    JPanel cp = new JPanel(); 
    cp.setLayout(null); 


    b1.setBounds(100,100,200,100); 
    cp.add(b1); 


    b2.setBounds(100,250,200,100); 
    cp.add(b2); 

    setContentPane(cp); 
    setVisible(true); 

} 

public static void main(String[] args) 
{ 
    Tema x = new Tema(); 
} 


@Override 
public void actionPerformed (ActionEvent e){   

} 

} 

但输出是这样的: enter image description here

我的问题是,为什么没有第二个按钮等于第一个按钮上方的空间下方的空间?它们不应该都是100像素吗?

+7

_decided not to use a layout_ wrong decision:you ** want ** to use a LayoutManager,always。学习如何使用它们,f.i.在swing标签wiki中引用的教程中:-) – kleopatra

回答

7
  • 请勿不必要地延伸JFrame类。
  • 永不使用Absolute/NullLayoutManager。使用适当的LayoutManager,这包括嵌套布局以实现期望的外观。在这里看到很好的教程:
  • 别叫JFrame#setSize(..)JFrame而设置JFrame可见之前只需要调用JFrame#pack()
  • 不使用JFrame#setContentPane(...)只使用add(..)JFrame例如
  • 创建Event Dispatch Thread初始化并更改UI组件
  • 不要为多个部件实现单ActionListener。除非其他课程或组件会访问它,否则它们共享Action。而是使用Anonymous ActionListener

这里是我做出了榜样(固定基本上你的代码)希望它帮助:

enter image description here

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

public class LayoutTest { 

    private final JMenuBar menubar = new JMenuBar(); 
    private final JMenu actiuni = new JMenu("Actiuni"); 
    private final JMenu contact = new JMenu("Contact"); 
    private final JMenu help = new JMenu("Help"); 
    private final JMenuItem ntest = new JMenuItem("Nou test"); 
    private final JMenuItem vizarh = new JMenuItem("Vizualizare arhiva"); 
    private final JMenuItem datcon = new JMenuItem("Date de contact"); 
    private final JMenuItem sendmail = new JMenuItem("Trimite e-mail"); 
    private final JMenuItem instrut = new JMenuItem("Instructiuni de utilizare"); 
    private final JButton b1 = new JButton("Incepe testul!"); 
    private final JButton b2 = new JButton("Vezi arhiva!"); 
    private final JTextArea ta = new JTextArea("Default text", 5, 30); 
    //create JFrame instance 
    private final JFrame frame = new JFrame(); 

    public LayoutTest() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     //creat UI on EDT 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new LayoutTest(); 
      } 
     }); 
    } 

    private void initComponents() { 
     frame.setTitle("Self-Esteem- Fereastra Principala"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     menubar.add(actiuni); 
     menubar.add(contact); 
     menubar.add(help); 
     actiuni.add(ntest); 
     actiuni.add(vizarh); 
     contact.add(datcon); 
     contact.add(sendmail); 
     help.add(instrut); 

     frame.setJMenuBar(menubar); 
     JPanel textAreaJPanel = new JPanel(); 

     //create button panel with GridLayout(2,1) 
     JPanel buttonJPanel = new JPanel(new GridLayout(2, 1));//new GridLayout(2, 1,10,10) creates gridlayout with horixontal and vertcial spacing of 10 

     //add buttons to one panel 
     buttonJPanel.add(b1); 
     buttonJPanel.add(b2); 
     //add text area to textarea jPanel 
     textAreaJPanel.add(ta); 

     //add textarea panel to west of content pane (BorderLayout by default) 
     frame.add(textAreaJPanel, BorderLayout.WEST); 

     //add button Panel to EAST of JFrame content pane 
     frame.add(buttonJPanel, BorderLayout.EAST); 

     frame.pack(); 

     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 

     frame.setVisible(true); 
    } 
} 
-2

在上面的代码空间这两个按钮之间, 50px,如果你使用b2.setBounds(100,300,200,100);那么空间将是100px。试试吧......

+4

此论坛不倾向于使用AbsoluteLayout,这会在Swing GUI中导致一些不正确的GPU不足,必须使用来自容器附近的Insets, – mKorbel

相关问题