简体   繁体   中英

unable to get java swing components

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;

public class Fram {
    public static void main(String[] args) {
        JTextArea map;
        JButton btn;
        map= new JTextArea();
        btn= new JButton("hello");
        JFrame frame= new JFrame("jarvis");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.setLayout(new BorderLayout());
        frame.add(map, BorderLayout.CENTER);
        frame.add(btn, BorderLayout.SOUTH);
    }
}

I am using this code to get a text area but I only get the frame without any textarea or button.

Your code works for me. I've added a couple of things you left out.

First, you must start all Swing applications with a call to SwingUtilities invokeLater. This ensures that your Swing components are defined and executed on the Event Dispatch thread (EDT).

Second, I moved the frame setVisible method last. You need to completely set up your JFrame before you make it visible.

Here's the modified code:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Fram implements Runnable {

    @Override
    public void run() {
        JTextArea map;
        JButton btn;
        map = new JTextArea();
        btn = new JButton("hello");
        JFrame frame = new JFrame("jarvis");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLayout(new BorderLayout());
        frame.add(map, BorderLayout.CENTER);
        frame.add(btn, BorderLayout.SOUTH);
        frame.setVisible(true);
    }

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


}

You didn't set size of buttons.It can be on center or south but you should declare it's size. so i prefer you btn.setBounds(0,0,100,20) if that wouldn't work you can delete BorderLayour.CENTER and set the bounds 200,280

Your code works fine to me as well. You mentioned you can not see swing component I think the reason you are not seeing your text area as expected because that text area is occupying the complete space in the border layout so you are not able to discern if you have the text area. So I have added a JPanel and JscrollPane in your code to improve the behavior. I would also like to call setVisible as last method and as mention by Gilbert you you must start all Swing applications with a call to SwingUtilities invokeLater.

package stackOverFlow;

import java.awt.BorderLayout;

public class Frame implements Runnable {
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Frame());
    }

    @Override
    public void run() {
        JTextArea map;
        JButton btn;
        JLabel lbl;
        map = new JTextArea(10, 50);
        final JScrollPane scrollPanel = new JScrollPane(map);
        btn = new JButton("hello");
        lbl = new JLabel("Text Area: ");
        final JPanel actionPanel = new JPanel(
                new FlowLayout(FlowLayout.LEADING));
        actionPanel.add(lbl);
        actionPanel.add(scrollPanel);
        final JFrame frame = new JFrame("jarvis");
        frame.setLayout(new BorderLayout());
        frame.add(actionPanel, BorderLayout.CENTER);
        frame.add(btn, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }
}

the output of program is

在此处输入图片说明

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