【问题标题】:JButton and JTextFieldJButton 和 JTextField
【发布时间】:2021-12-16 10:25:19
【问题描述】:

怎么了? ImageIcon 和框架的大小工作正常。 但JTextFieldJButton 不是。

我需要解决方案。

import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Alkalmazás");
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setResizable(false);

        JTextField field = new JTextField();
        field.setBounds(40,250, 300,35);

        JButton button = new JButton(new ImageIcon("table.png"));
        button.setBounds(40,400, 250,25);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tf.setText(""something);
            }
        });
        frame.add(field);
        frame.add(button);
    }

}

【问题讨论】:

  • 您不能同时将JTextFieldJButton 添加到JFrame 的中心。 setVisible 方法应在所有其他方法之后调用。
  • 不要尝试使用 setBounds(...)。 Swing 旨在与布局管理器一起使用。默认情况下,JFrame 使用BorderLayout。阅读Layout Managers 上的 Swing 教程以获取更多信息。

标签: java swing jbutton jtextfield layout-manager


【解决方案1】:

您没有提到什么是“无法正常工作”,但您的代码存在一些错误:

  1. 不要给你的班级打电话Frame,这可能会让你或其他人对java.awt.Frame感到困惑,可能有用的是MyFrame

  2. 现在你的所有类都在main 方法中,它没有放在Event Dispatch Thread (EDT) 中,要解决这个问题,创建你的类的实例并调用方法createAndShowGUI(或任何你想命名的方法) ) 里面SwingUtilities.invokeLater()

    例如:

     public static void main(String args[]) {
         SwingUtilities.invokeLater(new MyFrame()::createAndShowGUI)
     }
    

    或者,如果使用 Java 7 或更低版本,请在第 2 点中使用 this answer 内的代码。

  3. setVisible(true) 应该是您代码中的最后一行,否则您可能会发现一些视觉故障,直到您将鼠标移到窗口上方或触发对组件的 repaint() 的调用之前,这些故障可能会得到解决。

  4. 您应该覆盖JPanelgetPreferredSize(...),而不是直接调用setSize(...),然后在JFrame 上调用pack(),请参阅此问题及其中的答案:Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

  5. 您将 2 个组件添加到 BorderLayoutCENTER,这是 JFrame 的默认布局管理器,还有其他 layout managers,您可以将它们组合成复杂的 GUI。

  6. setBounds(...) 可能意味着您正在使用null-layout,这似乎是创建复杂布局的最简单方法,但是如果您采用这种方法,您会发现自己处于this one 这样的情况下,最好让Swing 在您使用布局管理器时为您进行计算。更多信息,请阅读:Why is it frowned upon to use a null layout in Swing?

现在牢记以上所有提示,您可能有一个类似于此的代码:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MyFrame {
    private JFrame frame;
    private JPanel pane;
    private JTextField field;
    private JButton button;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MyFrame()::createAndShowGUI);
    }

    private void createAndShowGUI() {
        frame = new JFrame("Alkalmazás");
        pane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }
        };

        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));

        field = new JTextField(10);
        button = new JButton("Click me");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                field.setText("something");
            }
        });

        pane.add(field);
        pane.add(button);

        frame.add(pane);

        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

现在你有一个类似于这个的输出:

您希望JTextField 具有更“正常”的大小怎么办?喜欢这个:

您必须将field 嵌入到另一个JPanel 中(使用FlowLayoutJPanel 的默认布局管理器),然后将第二个JPanel 添加到pane,我是不要为此编写代码,因为我将把它作为练习留给您,以便您学习如何使用多个布局管理器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多