简体   繁体   中英

JButton and JTextField

What's wrong? imageicon and the frame's size are working properly. But the textfield and the button aren't.

I need the solution.

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

}

You didn't mention what's "not working properly", but there are a few errors with your code:

  1. Don't call your class Frame , it may confuse you or others about java.awt.Frame , something that may work would be MyFrame

  2. Right now all your class is inside the main method and it's not placed inside the Event Dispatch Thread (EDT) , to fix this, create an instance of your class and call a method createAndShowGUI (or whatever you want to name it) inside SwingUtilities.invokeLater()

    For Example:

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

    Or if using Java 7 or lower, use the code inside this answer in point #2.

  3. setVisible(true) should be the last line in your code, otherwise you may find some visual glitches that may be resolved until you move your mouse above your window or something that triggers the call to repaint() of your components.

  4. Instead of calling setSize(...) directly, you should override getPreferredSize(...) of your JPanel and then call pack() on your JFrame , see this question and the answers in it: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

  5. You're adding 2 components to the CENTER of BorderLayout , which is a JFrame 's default layout manager, there are other layout managers and you can combine them to make complex GUI's.

  6. setBounds(...) might mean that you're using null-layout , which might seem like the easiest way to create complex layouts, however you will find yourself in situations like this one if you take that approach, it's better to let Swing do the calculations for you while you use layout managers. For more, read: Why is it frowned upon to use a null layout in Swing?

With all the above tips now in mind, you may have a code similar to this one:

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

Now you have an output similar to this one:

在此处输入图片说明

What about you want the JTextField to have a more "normal" size? Like this one:

在此处输入图片说明

You'll have to embed field inside another JPanel (with FlowLayout (the default layout manager of JPanel )), and then add that second JPanel to pane , I'm not writing the code for that as I'm leaving that as an exercise to you so you learn how to use multiple layout managers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM