一些Swing组件未显示在JFrame上 [英] Some Swing Components not showing on JFrame

查看:71
本文介绍了一些Swing组件未显示在JFrame上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个严重的问题.当我尝试在我的JFrame上添加2个JRadioButton和一个JLabel时,它不起作用.它们没有显示,但是其他元素显示了.

I've got a serious problem. When I try to add 2 JRadioButtons and a JLabel on my JFrame, it doesn't work. They are not showing, but other elements do.

有趣的是,我之前添加的其他组件(也包括JLabel)在JFrame上可见.

The funny thing is, that other components which I added before (also JLabels) are visible on the JFrame.

所有swing组件都添加到容器面板"上,该面板是JFrame的内容面板.

All swing components are added over a container 'panel' which is the content pane of the JFrame.

我正在使用Java DK 7 u67.

I'm using Java DK 7 u67.

代码如下:(向下滚动至/*此代码无效*/)

Here's the code: (Scroll down to /*This code doesn't work */)

public class UDP_FileTransfer implements KeyListener, Runnable {

JFrame fenster;
Container panel;
JButton startBt, setPathBt;
JTextField portFeld, ipFeld, savePathFeld;
JLabel infoLbl, serverLbl, clientLbl;
JRadioButton server, client;

boolean typeIsServer = true;

int port = 4444;
long size = 0;

boolean serverStarted;

public static void main(String[] args){

    new UDP_FileTransfer();
}

public UDP_FileTransfer(){

    fenster = new JFrame();
    fenster.setLayout(null);
    fenster.addKeyListener(this);
    fenster.setTitle("UDP File Transfer - Server");
    fenster.setLocationRelativeTo(null);
    fenster.setResizable(false);
    fenster.setSize(400,150);

    panel = fenster.getContentPane();
    panel.setLayout(null);
    panel.addKeyListener(this);

    JLabel portLbl = new JLabel("Port: ");
    portLbl.setBounds(10,10, 50,20);
    panel.add(portLbl);

    ...
    ...
    ...

    infoLbl = new JLabel("Status: Starten Sie den Server zuerst!");
    infoLbl.setBounds(10, 70, 371,20);
    infoLbl.setBorder(new LineBorder(Color.black, 1));
    panel.add(infoLbl);

    /* This code doesn't work */

    JLabel typeLbl = new JLabel("Wählen Sie den Typ aus:");
    typeLbl.setBounds(10,125,200,20);
    panel.add(typeLbl);

    client = new JRadioButton("Client");
    client.setBounds(230,125,70,20);
    panel.add(client);

    client = new JRadioButton("Server");
    client.setBounds(320,125,70,20);
    panel.add(client);

    /* End of non-working code */

    fenster.setVisible(true);
    fenster.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

GUI如下所示:点击我

JRadioButtons 客户端&服务器和JLabel typeLbl 应该出现在带有边框的框下.

The JRadioButtons client & server and the JLabel typeLbl should appear under the box with the border.

我希望你们中的一个能帮助我...

I hope one of you can help me ...

推荐答案

首先,应将后一个变量更改为server.

Firstly, you should change your latter variable to server.

    client = new JRadioButton("Client");
    client.setBounds(230,125,70,20);
    panel.add(client);

    server = new JRadioButton("Server");
    server.setBounds(320,125,70,20);
    panel.add(server);

并更改框架尺寸

frame.setSize(450, 175);

这篇关于一些Swing组件未显示在JFrame上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆