将一个JPanel添加到另一个JPanel中,使用JButton并添加addActionListener。

huangapple go评论76阅读模式
英文:

Adding JPanel into another JPanel using JButton with addActionListener

问题

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Test extends JFrame {
    JPanel panel;
    JButton send;
    JTextField text;
    JPanel chatArea;
    boolean typing;

    Test() {
        setSize(365, 515);
        setLocation(50, 100);
        setLayout(null);

        panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 350, 60);
        panel.setBackground(new Color(90000000));
        add(panel);

        JLabel name = new JLabel("IRONMAN");
        name.setFont(new Font("SAN_SERIF", Font.PLAIN, 14));
        name.setForeground(Color.white);
        name.setBounds(110, 35, 120, 20);
        panel.add(name);

        text = new JTextField();
        text.setBounds(15, 430, 260, 40);
        text.setFont(new Font("SAN_SERIF", Font.PLAIN, 14));
        text.setForeground(Color.BLUE);
        add(text);

        chatArea = new JPanel();
        chatArea.setBounds(5, 65, 340, 350);
        add(chatArea);

        send = new JButton("Send");
        send.setBounds(280, 430, 65, 30);
        send.setBackground(new Color(200, 120, 255));
        send.setForeground(new Color(7, 95, 75));
        send.addActionListener(e -> {
            String message = "STARK: " + text.getText();
            JPanel p2 = formatLabel(message);
            chatArea.add(p2);
            text.setText("");
        });
        add(send);
    }

    private JPanel formatLabel(String message) {
        JPanel p3 = new JPanel();
        JLabel label1 = new JLabel("<html><p style=\"width: 150px\">" + message + "</p></html>");
        label1.setBackground(new Color(200, 120, 255));
        label1.setForeground(new Color(7, 95, 75));
        label1.setFont(new Font("SAN_SERIF", Font.PLAIN, 18));
        label1.setOpaque(true);
        label1.setBorder(new EmptyBorder(15, 15, 15, 70));
        p3.add(label1);
        return p3;
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setVisible(true);
    }
}
英文:

The commented codes are the problem. When I am using them, panels are added successfully, but I don't need these commented code anymore but same code is not working after I remove or comment those blocks.

Those codes that I have commented need to be removed. Without those commented codes, program runs but does not add panels. I use IntelliJ for my Java Project.

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Test extends JFrame{
JPanel panel;
JButton send;
JTextField text;
JPanel chatArea;
boolean typing;
Test(){
setSize(365,515);
setLocation(50,100);
setLayout(null);
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,350,60);
panel.setBackground(new Color(90000000));add(panel);
JLabel name = new JLabel(&quot;IRONMAN&quot;);
name.setFont(new Font(&quot;SAN_SERIF&quot;, Font.PLAIN,14));
name.setForeground(Color.white);
name.setBounds(110,35,120,20);panel.add(name);
text = new JTextField();
text.setBounds(15,430,260,40);
text.setFont(new Font(&quot;SAN_SERIF&quot;,Font.PLAIN,14));
text.setForeground(Color.BLUE);
//        Timer timer = new Timer(1, event -&gt; {
//            if (!typing){
//                name.setText(&quot;IRONMAN&quot;);
//            }
//        });
//        timer.setInitialDelay(2000);
//        text.addKeyListener(new KeyAdapter() {
//            @Override
//            public void keyPressed(KeyEvent e) {
//                name.setText(&quot;IRONMAN typing...&quot;);
//                timer.stop();
//                typing = true;
//            }
//            @Override
//            public void keyReleased(KeyEvent e) {
//                typing = false;
//                if (!timer.isRunning()){
//                    timer.start();
//                }
//            }
//        });
add(text);
chatArea = new JPanel();
chatArea.setBounds(5,65,340,350);
add(chatArea);
send = new JButton(&quot;Send&quot;);
send.setBounds(280,430,65,30);
send.setBackground(new Color(200,120,255));
send.setForeground(new Color(7,95,75));
send.addActionListener(e -&gt; {
String message = &quot;STARK: &quot;+text.getText();
JPanel p2 = formatLabel(message);
chatArea.add(p2);
text.setText(&quot;&quot;);
});
add(send);
}
private JPanel formatLabel(String message) {
JPanel p3 = new JPanel();
JLabel label1 = new JLabel(&quot;&lt;html&gt;&lt;p style = \&quot;width : 150px\&quot;&gt;&quot; + message + &quot;&lt;/p&gt;&lt;/html&gt;&quot;);
label1.setBackground(new Color(200,120,255));
label1.setForeground(new Color(7,95,75));
label1.setFont(new Font(&quot;SAN_SERIF&quot;,Font.PLAIN,18));
label1.setOpaque(true);
label1.setBorder(new EmptyBorder(15,15,15,70));
p3.add(label1);
return p3;
}
public static void main(String[] args) {
Test t = new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}

答案1

得分: 1

首先,一些常见的评论:

label1.setBorder(new EmptyBorder(15, 15, 15, 70));

不要担心使用空格。例如:

label1.setBorder(new EmptyBorder(15, 15, 15, 70));

使用空格有助于提高文本的可读性。

setLayout(null);

不要使用空布局。Swing 设计时考虑了与布局管理器一起使用。您可以轻松使用 JFrame 的默认 BorderLayout。添加以下内容:

  1. 将顶部面板添加到 BorderLayout.PAGE_START
  2. 将聊天面板添加到 BorderLayout.PAGE_CENTER
  3. 将底部面板添加到 BorderLayout.PAGE_END

> 在我移除或注释这些代码块之后。

这段代码不是解决方案也不是问题所在。

问题在于组件的大小为 (0, 0),因此没有内容可以绘制。

在现有代码中,尝试通过扩大窗口的宽度来调整大小。面板将会出现。这是因为调整大小会触发布局管理器,从而为面板分配大小,以便可以绘制。

在您的代码中,您需要使用:

chatArea.add(p2);
chatArea.revalidate();

revalidate() 将自动调用布局管理器。

英文:

First off all some general comments:

label1.setBorder(new EmptyBorder(15,15,15,70));

Don't be afraid to use whitespace. For example

label1.setBorder(new EmptyBorder(15, 15, 15, 70));

It is easier for our eyes to see text with whitespace.

setLayout(null);

Don't use a null layout. Swing was designed to be used with layout managers. You can easily use the default BorderLayout of the JFrame. Add:

  1. the top panel to BorderLayout.PAGE_START
  2. the chat panel to BorderLayout.PAGE_CENTER
  3. the bottom panel to BorderLayout.PAGE_END

> after I remove or comment those blocks.

That code is not the solution or the problem.

The problem is that a component has a size of (0, 0) so there is nothing to paint.

In your existing code try resizing the frame by making it wider. The panel will appear. This is because the resizing will cause the layout manager to be invoked which will give the panel a size so it can be painted.

In your code you need to use:

chatArea.add(p2);
chatArea.revalidate();

The revalidate() will automatically invoke the layout manager.

huangapple
  • 本文由 发表于 2020年9月26日 03:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64070007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定