Java 如何在指定的布局中定位JLabel?
我试图在屏幕上定位我的标签和按钮。我在这里找到了一个设置标签位置的答案:label3.setHorizontalAlignment swingConstants.CENTER;标签3.SetVerticalAlignment SwingConstants.CENTER;但在我的代码中没有任何效果 另外,在将我的布局从流动布局更改为边框布局时,屏幕上只显示一个Jbutton[CLEAR]。有人能帮我纠正一下吗Java 如何在指定的布局中定位JLabel?,java,swing,jbutton,jlabel,layout-manager,Java,Swing,Jbutton,Jlabel,Layout Manager,我试图在屏幕上定位我的标签和按钮。我在这里找到了一个设置标签位置的答案:label3.setHorizontalAlignment swingConstants.CENTER;标签3.SetVerticalAlignment SwingConstants.CENTER;但在我的代码中没有任何效果 另外,在将我的布局从流动布局更改为边框布局时,屏幕上只显示一个Jbutton[CLEAR]。有人能帮我纠正一下吗 public class Corrected1 extends JFrame {
public class Corrected1 extends JFrame {
public JTextField tf1;
public JTextField tf2;
public JLabel label1;
public JLabel label2;
public JLabel label3;
public JLabel label4;
public JButton button1;
public JButton button2;
public JComboBox cb,cb1;
public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"};
public void init() {try {
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
Corrected();
}
});
} catch (InvocationTargetException exc) {
System.out.println("Error occured due to "+ exc);
exc.printStackTrace();
} catch (InterruptedException exc1) {
System.out.println("Error occured due to "+ exc1);
exc1.printStackTrace();
}
}
public void Corrected(){
setLayout(new GridBagLayout());
JLabel label1 = new JLabel("FROM :");
label1.setFont(new Font("Serif", Font.BOLD, 15));
JComboBox cb = new JComboBox();
cb= new JComboBox(list);
cb.getSelectedIndex();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(label1, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
add(cb, gbc);
JLabel label2 = new JLabel("TO :");
label2.setFont(new Font("Serif", Font.BOLD, 15));
JComboBox cb1 = new JComboBox();
cb1=new JComboBox(list);
cb1.getSelectedIndex();
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.NONE;
add(label2, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
add(cb1, gbc);
JLabel label3 = new JLabel("\n\nENTER THE AMOUNT :");
label3.setFont(new Font("Serif", Font.BOLD, 15));
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setVerticalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy++;
add(label3, gbc);
JTextField tf1 = new JTextField(15);
gbc.gridx++;
add(tf1, gbc);
JLabel label4 = new JLabel("CONVERTED AMOUNT :");
label4.setFont(new Font("Serif", Font.BOLD, 15));
label4.setDisplayedMnemonic(KeyEvent.VK_O);
gbc.gridx = 0;
gbc.gridy++;
add(label4, gbc);
JTextField tf2 = new JTextField(15);
gbc.gridx++;
add(tf2, gbc);
JPanel buttons = new JPanel();
JButton button1 = new JButton("CONVERT");
button1.setFont(new Font("Serif", Font.BOLD, 15));
button1.setMnemonic(KeyEvent.VK_K);
buttons.add(button1);
JButton button2 = new JButton("CLEAR ");
button2.setFont(new Font("Serif", Font.BOLD, 15));
button2.setMnemonic(KeyEvent.VK_C);
buttons.add(button2);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(buttons, gbc);
thehandler handler= new thehandler();
cb.addActionListener(handler);
cb1.addActionListener((ActionListener) handler);
button1.addActionListener((ActionListener) handler);
button2.addActionListener((ActionListener) handler);
tf1.addActionListener((ActionListener) handler);
tf2.addActionListener((ActionListener) handler);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//pack();
setSize(450,250);
setVisible(true);
}
public class thehandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
double a=0,b=0;
a=Double.valueOf(tf1.getText());
{
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1)
b=a*0.89;
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2)
b=a*124.75;
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3)
b=a*0.65;
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4)
b=a*64.08;
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0)
b=a*1.13;
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2)
b=a*140.49;
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3)
b=a*0.74;
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4)
b=a*71.34;
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0)
b=a*0.0080;
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1)
b=a*0.0071;
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3)
b=a*0.0052;
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4)
b=a*0.51;
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0)
b=a*1.53;
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1)
b=a*1.36;
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2)
b=a*191.26;
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4)
b=a*97.88;
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0)
b=a*0.0156;
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1)
b=a*0.014;
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2)
b=a*1.9607;
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3)
b=a*0.0108;
tf2.setText(String.valueOf(b));
}
if(e.getSource()==button2){
tf1.setText("0000");
tf2.setText("0000");
}
}
}
}
零部件定位相对于其所属的布局 如果您真的想自由设置JLabel和JButton的位置,我建议您将布局设置为null: setLayoutnull 并使用带有4个参数的立根来定位JLabel。JLabel的尺寸及其坐标。首先查看以了解其工作原理 BorderLayout只允许在其中定位五个组件,即北、南、东、西和中 默认情况下,如果未另行指定,零部件将放置在中心位置。这意味着只有最后添加的组件才实际布局 JLabelAuthorital/VerticalAlignment将在标签尺寸较大时对齐标签内容,但在FlowLayout的情况下,它使用标签内容的首选尺寸来决定如何最佳布局,因此通常没有额外的空间 相反,您可以使用GridBagLayout,这是一种更复杂但灵活的布局管理器
setLayout(new GridBagLayout());
JLabel label1 = new JLabel("FROM :");
label1.setFont(new Font("Serif", Font.BOLD, 15));
JComboBox cb = new JComboBox();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(label1, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
add(cb, gbc);
JLabel label2 = new JLabel("TO :");
label2.setFont(new Font("Serif", Font.BOLD, 15));
JComboBox cb1 = new JComboBox();
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.NONE;
add(label2, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
add(cb1, gbc);
JLabel label3 = new JLabel("\n\nENTER THE AMOUNT :");
label3.setFont(new Font("Serif", Font.BOLD, 15));
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setVerticalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy++;
add(label3, gbc);
JTextField tf1 = new JTextField(15);
gbc.gridx++;
add(tf1, gbc);
JLabel label4 = new JLabel("CONVERTED AMOUNT :");
label4.setFont(new Font("Serif", Font.BOLD, 15));
label4.setDisplayedMnemonic(KeyEvent.VK_O);
gbc.gridx = 0;
gbc.gridy++;
add(label4, gbc);
JTextField tf2 = new JTextField(15);
gbc.gridx++;
add(tf2, gbc);
JPanel buttons = new JPanel();
JButton button1 = new JButton("CONVERT");
button1.setFont(new Font("Serif", Font.BOLD, 15));
button1.setMnemonic(KeyEvent.VK_K);
buttons.add(button1);
JButton button2 = new JButton("CLEAR ");
button2.setFont(new Font("Serif", Font.BOLD, 15));
button2.setMnemonic(KeyEvent.VK_C);
buttons.add(button2);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(buttons, gbc);
有时,您甚至需要组合布局,我在这里已经这样做了,使用第二个JPanel,使用按钮的FlowLayout,因为它会产生所需的按钮居中结果
有关更多详细信息,请参见Null布局从来都不是解决方案,它只代表了新开发人员不需要的更多坏习惯和建议。避免使用空布局,像素完美布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计目的是与布局管理器一起工作,丢弃这些布局管理器将导致问题层出不穷,您将花费越来越多的时间试图纠正这些问题。有人可能想解释一下,这并不能确定OP存在的问题或提供解决问题的方法,为了吸引反对票?有趣的是,有一次我停止回答问题的两个原因之一,我非常个人化,因为我没有源代码,我能做的很少do@MadProgrammer我编辑了更正后的代码,没有可见错误,但它没有执行该功能。