Home > Enterprise >  How can I make this example with java GUI JFrame?
How can I make this example with java GUI JFrame?

Time:08-17

I want to make the tag (이름: 홍길동 학번: 1111111) looks like this,

이름:

홍길동

학번:

111111

but only I can make looks like this,

이름: 홍길동 학번: 111111

I made it to JLabel on SidePanel which extends JPanel. And \n is not working on JPanel I guess? ..and I don't know how to fix it.

Do I need to make some other JPanel on the SidePanel or use another Layout?? like.. Grid or null? or more JLabel??

Here's my code.

public class MyFrame extends JFrame {

private JButton proscons = new JButton();
private JLabel tag = new JLabel();
private JLabel num = new JLabel();

MyFrame() {
    setTitle("융프2 기말고사");
    
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(new WestPanel(), BorderLayout.WEST);
    cp.add(new MyPanel(), BorderLayout.CENTER);
    
    setLocationRelativeTo(null); // 가운데서 GUI 창 뜨도록
    setSize(400, 400);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class WestPanel extends JPanel {
    WestPanel() {
        setBackground(Color.YELLOW);
        setSize(100,400);
        add(proscons);
        proscons.setText("찬성");
        add(tag);
        tag.setText("이름: \n홍길동");
        add(num);
        num.setText("학번: \n11111111");
    }
}

class MyPanel extends JPanel {
    MyPanel() {
        setBackground(Color.lightGray);
    }
}

public static void main(String[] args) {
    new MyFrame();
}

}

CodePudding user response:

You can use HTML text formatting in JLabels.

Try doing this:

tag.setText("<html>이름:<br>홍길동</html>");
num.setText("<html>학번:<br>11111111</html>");
  • Related