【问题标题】:Scroll bar with GridLayout(1,1)带有 GridLayout(1,1) 的滚动条
【发布时间】:2017-10-11 11:24:27
【问题描述】:

在这里,我正在使用 Swing 编写 Java 程序,但遇到了一些麻烦。我可以在我的文本区域添加滚动条:

    //Text area
    public static JTextArea output = new JTextArea("======================== The Outputs HERE ================================");

   // JBouton 
   private JButton bouton = new JButton();
   private JButton tpBouton = new JButton("TP 205");

   // JLabel
   private JLabel label = new JLabel("TP DE 205");
   public static JLabel initialState = new JLabel("TEST");
   public static JLabel state = new JLabel("");
   public static JLabel tableShow = new JLabel("Entrer l'element a rechercher : ");
   public static JLabel finalState = new JLabel("");
   private JLabel explicitText = new JLabel();

   // JTextField
   private JFormattedTextField input = new JFormattedTextField(NumberFormat.getIntegerInstance());
    //JTextField input = new JTextField("Choisir l'element a rechercher ...");

   // JPanel
   JPanel mainBGRadio = new JPanel();
   JPanel secondBGRadio = new JPanel();
   JPanel b7 = new JPanel();
   JPanel main = new JPanel();
   JPanel container = new JPanel();
   JPanel outPutField = new JPanel();

   // Ensemble
   private Ensemble set = new Ensemble();

   // JRadioButton

   // for the main group
   private JRadioButton lookup = new JRadioButton("Look Up");
   private JRadioButton insert = new JRadioButton("Insert");
   private JRadioButton delete = new JRadioButton("Delete");
   private JRadioButton difference = new JRadioButton("Difference");
   private JRadioButton intersection = new JRadioButton("Intersection");
   private JRadioButton union = new JRadioButton("Union");

   // for the second group

   private JRadioButton sortedList = new JRadioButton("liste Trie");
   private JRadioButton simpleList = new JRadioButton("liste Simple");
   private JRadioButton doubleList = new JRadioButton("liste Avec Doublons");
   private JRadioButton simpleVector = new JRadioButton("vecteur Simple");
   private JRadioButton sortedVector = new JRadioButton("vecteur Trie");
   private JRadioButton booleanVector = new JRadioButton("vecteur Boolean");

    // JButtonGroup

    // some bouton group

   private ButtonGroup mainBG = new ButtonGroup();
   private ButtonGroup secondBG = new ButtonGroup();

   // ArrayList

   private ArrayList<JRadioButton> radioList = new ArrayList(){{
        add(lookup);
        add(insert);
        add(delete);
        add(union);
        add(intersection);
        add(difference);
    }};

    private ArrayList<JRadioButton> radioFunctionList = new ArrayList(){{
        add(sortedList);
        add(doubleList);
        add(simpleList);
        add(simpleVector);
        add(sortedVector);
        add(booleanVector);
    }};

    // Font

   Font font = new Font("ubuntu", Font.BOLD, 45);
   Font explicitFont = new Font("ubuntu", Font.BOLD, 15);
   Font stateFont = new Font("ubuntu", Font.BOLD, 25);
   Font textareaFont = new Font("ubuntu", Font.ITALIC, 19);


   public Fenetre(){

   }

/**
 * this is the main window with all its contains element
 */
public void montrer(){

        // window Params
        this.setTitle("TP 205");
        this.setSize(800, 800);
        this.setLocationRelativeTo(null);

        // bouton configuration

        bouton.setText("Simuler");
        bouton.addActionListener(new BtnClass());
        tpBouton.addActionListener(new BtnClass());
        tpBouton.setBackground(Color.BLACK);
        tpBouton.setFont(explicitFont);
        tpBouton.setForeground(Color.WHITE);


        // JLabel configuration

        explicitText.setText("Bienvenur sur notre tp de 205");
        label.setFont(font);
        explicitText.setFont(explicitFont);
        explicitText.setForeground(Color.WHITE);
        label.setForeground(Color.WHITE);
        state.setForeground(Color.WHITE);
        state.setFont(stateFont);
        initialState.setForeground(Color.WHITE);
        initialState.setFont(stateFont);
        finalState.setForeground(Color.WHITE);
        finalState.setFont(stateFont);

        // input configuration

        input.setFont(explicitFont);
        input.setPreferredSize(new Dimension(150, 30));
        input.setForeground(Color.BLACK);
        input.setMaximumSize(new Dimension(Integer.MAX_VALUE, input.getMinimumSize().height));
        output.setFont(textareaFont);
        output.setWrapStyleWord(true);
        output.setLineWrap(true);

        JScrollPane scrool = new JScrollPane();
        scrool.setBorder(BorderFactory.createTitledBorder("LIONEL"));
        scrool.setViewportView(output);

        // layout configuration

        JPanel b1 = new JPanel();
        b1.setLayout(new BoxLayout(b1, BoxLayout.LINE_AXIS));
        b1.add(label);

        JPanel b2 = new JPanel();
        b2.setLayout(new BoxLayout(b2, BoxLayout.LINE_AXIS));
        b2.add(explicitText);

        JPanel b3 = new JPanel();
        b3.setLayout(new BoxLayout(b3, BoxLayout.LINE_AXIS));
        b3.add(tpBouton);
        b3.add(bouton);

        JPanel b4 = new JPanel();
        b4.setLayout(new BoxLayout(b4, BoxLayout.LINE_AXIS));
        b4.add(state);

        JPanel b5 = new JPanel();
        b5.setLayout(new BoxLayout(b5, BoxLayout.LINE_AXIS));
        b5.add(finalState);

        JPanel b6 = new JPanel();
        b6.setLayout(new BoxLayout(b6, BoxLayout.LINE_AXIS));
        b6.add(initialState);

        b7.setLayout(new BoxLayout(b7, BoxLayout.LINE_AXIS));
        b7.add(tableShow);
        b7.add(input);
        b7.setVisible(false);

        this.setEventSimpleRadio();
        this.setEventSecondRadio();
        secondBGRadio.setVisible(false);
        mainBGRadio.setLayout(new BoxLayout(mainBGRadio, BoxLayout.LINE_AXIS));
        secondBGRadio.setLayout(new BoxLayout(secondBGRadio, BoxLayout.LINE_AXIS));
        container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
        main.setLayout(new GridLayout(2, 1));

        b1.setBackground(Color.ORANGE);
        b2.setBackground(Color.ORANGE);
        b3.setBackground(Color.ORANGE);
        b4.setBackground(Color.ORANGE);
        b5.setBackground(Color.ORANGE);
        b6.setBackground(Color.ORANGE);
        b7.setBackground(Color.ORANGE);


        mainBGRadio.setBackground(Color.ORANGE);
        secondBGRadio.setBackground(Color.ORANGE);

        container.add(b1);
        container.add(b2);
        container.add(mainBGRadio);
        container.add(secondBGRadio);
        container.add(b7);
        container.add(b6); 
        container.add(b4); 
        container.add(b5);

        container.add(b3);
        outPutField.add(output);
        outPutField.add(scrool);
        outPutField.setLayout(new GridLayout(0,1, 5, 5));
        container.setBackground(Color.ORANGE);
        main.add(container);
        main.add(outPutField);


        this.setContentPane(main);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

这是它产生的屏幕截图:

我正在寻找如何解决这个问题并在文本区域溢出时正确显示滚动条并有一个小滚动条。

【问题讨论】:

  • 请发一个有效的minimal reproducible example。你的代码有太多不需要的代码,包括一大堆不相关的组件,以及没有足够的代码——没有足够的代码让我们编译或运行它。也不要将您的 JTextfield 添加到多个容器中。您将它添加到视口(好)和 JPanel(坏)。最后一点会将其从视口中移除,这可能是您看不到它的原因。
  • 换句话说,去掉这行:outPutField.add(output);,因为它把你搞砸了。
  • thakns ...问题是...程序有4个类文件,每个类文件有200多行...我没有在这里导入它的原因...
  • 再一次,我们不想看到整个程序。您应该创建一个新的小型minimal reproducible example 程序。请阅读链接之前回复。

标签: java swing jscrollpane layout-manager jtextarea


【解决方案1】:

使用 JTextArea 的基本逻辑是:

//public static JTextArea output = new JTextArea("==== The Outputs HERE =====;
public static JTextArea output = new JTextArea(5, 30);

这将允许您创建一个包含 5 行数据和每行大约 30 个字符的文本区域。一旦你有超过 5 行的数据,滚动条就会出现。

然后在你的构造函数中你可以指定默认文本:

output = "========= The Outputs HERE ================================");

此外,您不应该在所有变量上使用 static 关键字。这表明您的班级设计不正确。阅读 How to Use Text Area 上的 Swing 教程中的部分,了解一个工作示例,该示例将向您展示如何更好地设计您的类。

保留所有 Swing 基础知识的教程链接。

【讨论】:

  • 谢谢。我这样做是因为许多其他子类使用其他类中的变量进行静态开发
  • @LionelT, thakns . - 很高兴它有帮助。不要忘记通过单击复选标记“接受”答案,以便人们知道问题已解决。
  • i have done it like that because... - 这又是一个糟糕的设计。这不是 static 关键字的使用方式。本教程向您展示了一个更好的设计。学习更好的设计方法,让您的代码更易于维护和调试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多