2016-04-20 37 views
0

我遇到了一個嚴重的問題。當我嘗試在我的JFrame上添加2個JRadioButton和一個JLabel時,它不起作用。他們沒有顯示,但其他元素呢。JFrame上沒有顯示某些Swing組件

有趣的是,我之前添加的其他組件(也是JLabels)在JFrame上可見。

將所有擺動組件添加到作爲JFrame的內容窗格的容器「面板」上。

我正在使用Java DK 7 u67。

下面的代碼:(向下滾動到/*這個代碼不工作*/

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看起來是這樣的:Click me

的JradioButton將客戶&服務器並且JLabel typeLbl應該出現在帶邊框的框下。

我希望你可以幫我...

+2

不要設置您的組件的邊界。這是佈局經理的角色。使用佈局管理器。 –

+0

'fenster.setLayout(null);'1)Java GUI必須在不同的語言環境中使用不同的PLAF來使用不同的OS,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 2)以最小尺寸提供ASCII藝術或簡單的GUI圖形*圖形,並且如果可調整大小,則具有更大的寬度和高度。 –

回答

0

首先,你應該將後者變量更改爲服務器。

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