[英]trouble with size and positions of components in swing
我制作了一個簡單的java swing程序,但是我在組件的大小和位置上遇到了麻煩。 我有三個Jlabels和三個JtextFields。 jlabel是重疊的,因此僅顯示我添加的最后一個。 文本字段顯示的很小。(幾乎看起來像是行。)而且,所有內容都顯示在一行上,我希望每個組件都在自己的行上。
碼:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class setCustoms {
static JPanel panel = new JPanel();
static JFrame frame2 = new JFrame("Settings");
public static void makeWindow(){
frame2.setVisible(true);
frame2.add(panel);
frame2.setBounds(500, 300, 500, 200);
JLabel app1l = new JLabel("");
JLabel app2l = new JLabel("");
JLabel app3l = new JLabel("");
JLabel site1l = new JLabel("");
JLabel site2l = new JLabel("");
JLabel site3l = new JLabel("");
panel.add(app1l);
app1l.setText("Set Application ONE name and path");
JTextField app1t = new JTextField();
app1t.setBounds(100, 100, 100, 25);
app1t.addActionListener(new Action());
panel.add(app1t);
panel.add(app2l);
app1l.setText("Set Application TWO name and path");
JTextField app2t = new JTextField();
app2t.addActionListener(new Action());
panel.add(app2t);
panel.add(app3l);
app1l.setText("Set Application THREE name and path");
JTextField app3t = new JTextField();
app3t.addActionListener(new Action());
panel.add(app3t);
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e){
}
}
}
編輯:althoguh我確實有代碼來設置某些項目的范圍,它不起作用
您需要使用LayoutManager(布局管理器的可視指南)來組織組件。
GridLayout的示例:
1)在JFrame中配置GridLayout
// with 3 rows and 2 columns
static JPanel panel = new JPanel(new GridLayout(3, 2));
2)按從左到右從下到下的順序創建和添加元素
JLabel app1l = new JLabel("");
app1l.setText("Set Application ONE name and path");
panel.add(app1l);
JTextField app1t = new JTextField();
panel.add(app1t);
// (...)
//keep adding your elements in order
3)打包您的JFrame以整理所有添加的元素
frame3.pack();
正如MadProgrammer指出的那樣,這些元素顯示在一行中,因為您沒有為panel
設置布局管理器。 按照PedroVítor的建議,您可以使用網格布局 。 例如,對於單列布局,您可以執行以下操作:
static JPanel panel = new JPanel (new GridLayout (0,1));
其中0
表示沒有行,而1
表示列數。
仔細看看您的代碼。 實際發生的是,您一直都在只為app1l
設置文本。 其他標簽在那里。 只是他們沒有文字。 可能是復制粘貼的產品;)
正如PedroVítor所述,您應該養成按順序添加元素的習慣。 回到代碼時,這將避免您的頭痛。 此外,我建議您僅在添加所有組件之后才使框架可見。 否則,不能保證將渲染所有這些組件。 看一下這個問題: 為什么在添加組件之前不應該調用setVisible(true)?
因此,綜合考慮,您的代碼應如下所示:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class setCustoms {
static JPanel panel = new JPanel(new GridLayout(0,1));
static JFrame frame2 = new JFrame("Settings");
public static void main (String[] args){
JLabel app1l = new JLabel("");
JLabel app2l = new JLabel("");
JLabel app3l = new JLabel("");
app1l.setText("Set Application ONE name and path");
panel.add(app1l);
JTextField app1t = new JTextField();
app1t.setBounds(100, 100, 100, 25);
app1t.addActionListener(new Action());
panel.add(app1t);
app2l.setText("Set Application TWO name and path");
panel.add(app2l);
JTextField app2t = new JTextField();
app2t.addActionListener(new Action());
panel.add(app2t);
app3l.setText("Set Application THREE name and path");
panel.add(app3l);
JTextField app3t = new JTextField();
app3t.addActionListener(new Action());
panel.add(app3t);
frame2.add(panel);
frame2.setBounds(500, 300, 500, 200);
frame2.setVisible(true);
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e){
}
}
}
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.