【发布时间】:2012-10-13 09:44:48
【问题描述】:
我在这里有这段代码,想法是在主窗口中在文本区域旁边有两个按钮,我还没有添加。在尝试使用 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){
}
}
但是输出是这样的:
我的问题是为什么第二个按钮下方的空间不等于第一个按钮上方的空间?它们不应该都是 100 像素吗?
【问题讨论】:
-
决定不使用布局 错误的决定:您总是想要使用 LayoutManager。了解如何使用它们,f.i.在swing tag wiki中引用的教程中:-)
标签: java swing user-interface jbutton layout-manager