如何在指定的布局中放置JLabel? [英] How to Position a JLabel in the specified Layout?

查看:140
本文介绍了如何在指定的布局中放置JLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的标签和按钮放在屏幕上.我在这里找到了使用label3.setHorizontalAlignment(SwingConstants.CENTER); label3.setVerticalAlignment(SwingConstants.CENTER);设置标签的位置的答案,但是在我的代码中没有任何作用.

I am trying to position my Labels and Button on screen. I found an answer here to set label's Position using: label3.setHorizontalAlignment(SwingConstants.CENTER); label3.setVerticalAlignment(SwingConstants.CENTER); but there has been no effect in my code.

此外,在将我的布局从流布局"更改为边界布局"时,屏幕上仅出现一个Jbutton [CLEAR].谁能帮我纠正这个问题?

Also, on changing my layout from Flow Layout to Border Layout, only one Jbutton [CLEAR] appears on the Screen. Can anyone help me rectify this?

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");
                        }

    }

}

}

推荐答案

首先查看如何使用BorderLayout 了解其工作原理.

Start by taking a look at How to Use BorderLayout to understand how it works.

BorderLayout仅允许在其中放置五个组件,NORTHSOUTHEASTWESTCENTRE

BorderLayout will only allow five components to positioned within it, NORTH, SOUTH, EAST, WEST and CENTRE

默认情况下,如果没有另外指定,组件将放置在CENTRE位置.这意味着只有最后添加的组件才被实际布置.

By default, components are placed in the CENTRE position if you don't otherwise specify. This means that only the last component added is actually laid out.

JLabel#setHorizontal/VerticalAlignment将在标签的大小较大时对齐标签的内容,但在FlowLayout的情况下,它将使用标签内容的首选大小来决定如何最佳布局,因此通常没有多余的空间

JLabel#setHorizontal/VerticalAlignment will align the contents of the label when the size of the label is larger, but in the case of FlowLayout, it uses the preferred size of the label's content to make decisions about how best to lay it out, so there's generally no extra space

相反,您可以使用GridBagLayout,它是可用的更为复杂但灵活的布局管理器之一.

Instead, you could use a GridBagLayout, which a one of the more complicated, but flexible layout managers available.

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);

有时候,您甚至需要组合布局,这是我在这里所做的,使用第二个JPanelFlowLayout作为按钮,因为这样可以产生使按钮居中的理想结果

Sometimes, you even need to combine layouts, which I've done here, using a second JPanel, using a FlowLayout for the buttons, because it produces the desired result of centring the buttons

有关更多详细信息,请参见如何使用GridBagLayout

See How to Use GridBagLayout for more details

这篇关于如何在指定的布局中放置JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆