【问题标题】:Location set up with GridBagLayout is not going good & JTextField & JButton wont take affect使用 GridBagLayout 设置的位置不太好,JTextField 和 JButton 不会生效
【发布时间】:2013-11-23 10:48:05
【问题描述】:

所以当我遇到这个问题时,我正在开发这个小程序。它主要是关于使用 GridBagLayout 设置的位置,它不会向我显示 JLable 的文本。另一个问题是,我的进度是工作停止工作,后来又回来了。知道它是什么吗?我也想要有人帮我解决位置问题我想把 JTextField 放在左下角有什么帮助吗?我无法解决这个问题,也没有信息在线专门帮助我。所以也许你可以帮助我。她的代码到目前为止......

package Main;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;


public class code extends JFrame{

    public static JTextField consol;
    public static String title = "Metal-Lock:The Start";
    public static Dimension size = new Dimension(650, 550);
    public static JPanel panel;
    public static JButton enter;
    public static JLabel output;

    public static void main(String args[]) {
        code frame = new code();
    }
        public code() {
            setTitle(title);
            setSize(size);
            setResizable(true);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);

        //  VISITOR LIST
            CONSOL();
            P1();
            P2();
}
//******************************************************************************************************************************

        public void CONSOL() {
                consol = new JTextField(30);

                consol.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                        String input = consol.getText(); 
                        output.setText(input); 
                    }});
                final JButton enter = new JButton("Enter");
                 enter.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                        String input = consol.getText(); 
                        output.setText(input);
                        panel.add(consol);
                        add(panel);

                        panel.add(enter);
                        add(panel);

                        output = new JLabel();
                        panel.add(output);
                        add(panel);
                    }
                });      
                }

//******************************************************************************************************************************
       public void P1() {

       }

//******************************************************************************************************************************
       public static JLabel grid1;
       public static JLabel grid2;
       public static JLabel grid3;
       public static JLabel grid4;
       public static JLabel grid5;

       public void P2() {

           JPanel panel = new JPanel(new GridBagLayout());
           GridBagConstraints R = new GridBagConstraints();

       JLabel grid1 = new JLabel ("Hello");  panel.add(grid1, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid2 = new JLabel ("Hello");  panel.add(grid2, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid3 = new JLabel ("Hello");  panel.add(grid3, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid4 = new JLabel ("Hello");  panel.add(grid4, R);
       R.gridx = 0; R.gridy = 0;
       JLabel grid5 = new JLabel ("Hello");  panel.add(grid5, R);
       R.gridx = 0; R.gridy = 0;

       }
}

【问题讨论】:

  • 在容器中的哪里添加组件??
  • 如果您使用的是 Eclipse,您可以使用 UI 编辑器。对于实例 WindowBuilder。之后你也可以从生成的代码中学习。
  • 请您 Gábor Csikós 向我展示窗口构建器,但现在我必须使输入按钮工作,以便它显示我输入的内容。她喜欢我的新问题! stackoverflow.com/questions/20169052/…
  • 无关:请学习java命名约定并遵守它们

标签: java swing


【解决方案1】:

刚刚快速浏览了您的代码。

  1. 更改这些行:

    GridBagConstraints R = new GridBagConstraints();
    R.gridx = 0; R.gridy = 0;    
    JLabel grid1 = new JLabel ("Hello");
    //important to set these R values BEFORE you ad grid1.
    panel.add(grid1, R);
    

    在所有行中更改它...

  2. 您将 6 个标签全部添加到位置 gridx=0 和 gridy=0,这是错误的。想象一下它就像一个 Excel 表格,您将每个标签插入到字段 1。

    如果您想添加字段,请这样做

    例如

x y
z

//a.gridx=0; a.gridy=0;
//y.gridx=1; y.gridy=0;
//z.gridx=0; z.gridy=1;

x 坐标是水平的。
y 从左上角开始垂直。
  1. 看了这篇文章,真的很不错:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2016-11-27
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多