【问题标题】:JButton PositioningJButton 定位
【发布时间】:2013-10-17 05:41:41
【问题描述】:

好的,所以我正在尝试定位我的 JButton,但如果我将“this.setLayout”设置为 null,我的按钮不会显示,但如果我放置一个布局,按钮就会出现。我真的想定位我的按钮而不是使用布局.. 我试过使用一个容器,一个面板(见下文),只是经常......没有任何作用:l

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

public class BingoHelper extends JFrame implements WindowListener, ActionListener{
    JTextField text = new JTextField(20);
    JPanel pnlButton = new JPanel();
    private JButton b; {
            b = new JButton("Click to enter name");
            }

    public void actionPerformed (ActionEvent e) {
        String fn = JOptionPane.showInputDialog("Username:");
        String sn = JOptionPane.showInputDialog("Password:");
        JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", JOptionPane.INFORMATION_MESSAGE);
        text.setText(fn + " " + sn);
        b.setVisible(false);
        text.setVisible(true);
    } 

    public BingoHelper(){
        super("BINGO");
        this.setLayout(new GridBagLayout());
        add(text);
        text.setVisible(false);
        this.add(pnlButton);
        pnlButton.add(b);
        pnlButton.setVisible(true);
        pnlButton.setLocation(800,800);
        b.setVisible(true);
        b.setPreferredSize(new Dimension(150,40));
        b.addActionListener(this);
    }

    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);

    }
    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
}

【问题讨论】:

  • if I set the "this.setLayout" to null... - 不要使用空布局!!! Swing 旨在与layout managers 一起使用。因此,与不同的布局管理器一起玩,看看它们是如何工作的,记住你也可以嵌套布局管理器。
  • 我真的想定位我的按钮,而不是使用布局。不,你不需要。也许你还没有意识到,但你真的没有

标签: java swing layout jpanel jbutton


【解决方案1】:

当您是using Null Layout 时,您必须使用button.setBounds(x, y, width, height); 方法定位JButton,其中(x, y) 是按钮位置,(width, height) 是按钮的大小。现在让我们检查您的代码:

        add(text);
        text.setVisible(false);
        //this.add(pnlButton);
        pnlButton.setLayout(null); // setting the null layout to the button panel
        pnlButton.add(b);      
        //pnlButton.setVisible(true); // why do you need it ? it is already true
        //pnlButton.setLocation(800,800);  // weired location, remove it 
        //b.setVisible(true);    // why do you need it? it is already true
        //b.setPreferredSize(new Dimension(150,40));
         b.setBounds(20, 20, 100, 30); // putting setBounds here
        b.addActionListener(this);

         setContentPane(pnlButton);   //set the pnlButton as content pan

    }

【讨论】:

  • 虽然技术上正确,但不使用 LayoutManager 是不行的
  • 是的,这就是为什么我附上了一个 JAVA DOC 源代码,它就是这么说的。
【解决方案2】:

您使用的是 GrigBagLayout,您应该使用 GridBagConstraints 来添加和定位组件的位置。

有关 Grigbaglayout 的更多信息,请参阅链接:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多