【问题标题】:How to add a JPanel object, with components, to a JFrame如何将带有组件的 JPanel 对象添加到 JFrame
【发布时间】:2023-03-28 18:45:01
【问题描述】:

我想将 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;
    }
}

图片:

【问题讨论】:

  • 并且没有异常创建JScrollPane?
  • 一般建议:1) 不要扩展JFrameJPanel,除非有必要这样做。这两个类都不需要。 2) 对类使用描述性名称,并学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。 First_view 应该是 FirstViewP2 应该类似于 ApplicationContainer。 3) 为了尽快获得更好的帮助,请发布minimal reproducible exampleShort, Self Contained, Correct Example。 ..
  • .. 4) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。 5) 源代码中的一个空白行是永远需要的。 { 之后或} 之前的空行通常也是多余的。
  • 忽略 MCVE 的评论 - 我没有注意到 P5 的定义包含在内,但我现在不会删除评论并重新编号。 :P 6) JTextArea T1 = new JTextArea(); 应该更像 JTextArea T1 = new JTextArea(20,3); // suggest a size in columns & rows

标签: java swing jpanel


【解决方案1】:

JScrollPane 中显示的组件应该添加,请改用setViewportView

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

    informacion.setViewportView(T1);

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

Obs:传递给JScrollPane的构造函数的参数顺序错误,即vertical警察先来:

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

编辑:正如 Andrew 所说,扩展一个类只是为了使用它不是一个好主意(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);
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多