简体   繁体   English

无法获取java swing组件

[英]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.首先,您必须通过调用 SwingUtilities invokeLater 来启动所有 Swing 应用程序。 This ensures that your Swing components are defined and executed on the Event Dispatch thread (EDT).这可确保您的 Swing 组件在事件调度线程(EDT) 上定义和执行。

Second, I moved the frame setVisible method last.其次,我最后移动了框架 setVisible 方法。 You need to completely set up your JFrame before you make it visible.在使其可见之前,您需要完全设置 JFrame。

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所以我更喜欢你btn.setBounds(0,0,100,20)如果那不起作用你可以删除 BorderLayour.CENTER 并设置边界 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.您提到you can not see swing component我认为您没有按预期看到文本区域的原因是因为该文本区域占据了边框布局中的完整空间,因此您无法辨别是否有文本区域。 So I have added a JPanel and JscrollPane in your code to improve the behavior.因此,我在您的代码中添加了 JPanel 和 JscrollPane 以改进行为。 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.我还想调用 setVisible 作为最后一个方法,正如 Gilbert 所提到的,您必须通过调用 SwingUtilities invokeLater 来启动所有 Swing 应用程序。

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程序的输出是

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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