0

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.

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

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

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

The GUI looks like this: Click me

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

I hope one of you can help me ...

Michael Gierer
  • 401
  • 2
  • 6
  • 15
  • 2
    Don't set bounds of your components. That's the role of the layout manager. Use a layout manager. – JB Nizet Apr 20 '16 at 17:47
  • `fenster.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Apr 20 '16 at 23:56

1 Answers1

0

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

and change your frame size

frame.setSize(450, 175);
ziLk
  • 3,120
  • 21
  • 45