gpt4 book ai didi

java - 如何将带有组件的 JPanel 对象添加到 JFrame

转载 作者:行者123 更新时间:2023-11-29 04:37:20 25 4
gpt4 key购买 nike

我想将 JPanel 类型的对象添加到 JFrame。

我正在尝试这个,但是没有添加 Jpanel。

想法是:向 P2 添加一个 P5,该 P5 具有类 P5 中定义的组件。

会发生什么?,我不想在类 First_view 中创建所有 JPanel,因为代码会很困惑。

代码:

import javax.swing.*;
import java.awt.*;

public class First_view extends JFrame {

Container Frame;

public First_view() {
super("title");
Frame = this.getContentPane();
Frame.setLayout(new BorderLayout());

Frame.add((new P2()), BorderLayout.WEST);

setSize(900, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

class P2 extends JPanel {

public P2() {
this.setLayout(new BorderLayout());

add((new P5()), BorderLayout.NORTH);
}
}

class P5 extends JPanel {

JScrollPane informacion = new JScrollPane(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTextArea T1 = new JTextArea();

public P5() {
setLayout(new FlowLayout());
setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 0));
add(setInformacion());
}

private JScrollPane setInformacion() {
T1.append("Information, bla bla bla");

T1.setEditable(false);
T1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
T1.setLineWrap(true);
informacion.add(T1);
informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return informacion;
}
}

图片:

enter image description here

最佳答案

要在 JScrollPane 中显示的组件应该 添加,而是使用 setViewportView

private JScrollPane setInformacion() {
T1.append("Information, bla bla bla");
...

informacion.setViewportView(T1);

...
informacion.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return informacion;
}

观察:传递给 JScrollPane 构造函数的参数顺序错误,即 vertical police 在前:

JScrollPane informacion = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

编辑:正如安德鲁评论的那样,扩展一个类只是为了使用它(JFrame、JPanel)并不是一个好主意。例如,我尽量不对您的原始流程进行太多更改: 包 cfh.test;

import javax.swing.*;
import java.awt.*;


public class FirstView {

private JFrame frame;
private JTextArea informacionText; // not sure if that is needed as field

public FirstView() {
informacionText = new JTextArea();
informacionText.setEditable(false);
informacionText.setBorder(BorderFactory.createLineBorder(Color.BLACK));
informacionText.setLineWrap(true);
informacionText.append("Information, bla bla bla");


JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
scrollPane.setViewportView(informacionText);


JPanel infoPanel = new JPanel();
infoPanel.setLayout(new FlowLayout());
infoPanel.add(scrollPane);


JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
leftPanel.add(infoPanel, BorderLayout.NORTH);
// TODO consider moving above code to an own method returning the left panel

frame = new JFrame("title");

frame.setLayout(new BorderLayout());
frame.add(leftPanel, BorderLayout.WEST);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

关于java - 如何将带有组件的 JPanel 对象添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40782424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com