【问题标题】:put two vertical JSplitPane on a frame java将两个垂直的 JSplitPane 放在一个框架 java 上
【发布时间】:2020-07-08 11:52:45
【问题描述】:

我是一名新的 Java 程序员,从一开始就使用 stackoverflow。我编写了一个小“游戏”,它是一个基于文本的游戏。好吧,我开始一个图形界面,对文本进行大小写,我会有这个配置

基本上,它是双重分隔,具有 3 个水平元素。其实我有这个:

我想分手

我尝试在第一个窗格的顶部放置另一个拆分窗格,如下所示:

package sample;

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

class Fenetresaisie extends JFrame {
    public static class Fenetre {
        public final static int HT = 1024;
        public final static int LG = 758;

        public static void main(String[] args) {
            JPanel panel = new JPanel();
            JFrame F = new JFrame("CORONAZE");
            F.setExtendedState(JFrame.MAXIMIZED_BOTH);
            F.setSize(HT, LG);
            F.setVisible(true);

            F.addWindowListener(new gestionFenetre());

            ImageIcon icone = new ImageIcon("images.jpg");

            JLabel image = new JLabel(icone);

            JTextField textField = new JTextField();
            textField.setFont(new Font("Terminal", Font.BOLD, 30));
            textField.setForeground(Color.RED);
            textField.setBackground(Color.black);

            textField.addKeyListener(new java.awt.event.KeyAdapter() {

                public void keyReleased(java.awt.event.KeyEvent e) {
                     textField.getText();
                     e.getKeyChar();
                }
            });

            JLabel label = new JLabel(">texte de l'histoire ici<");
            label.setOpaque(true);
            label.setForeground(Color.green);
            label.setBackground(Color.BLACK);
            panel.add(label);

            JSplitPane topJSplitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, label, textField);
          //  topJSplitPane.setDividerLocation(400);


            JSplitPane bottomJSplitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, topJSplitPane, textField );
            //i added it to have a double separation, but it give 2 sticked splitpane
            F.add(topJSplitPane, BorderLayout.CENTER);
            F.add(bottomJSplitPane, BorderLayout.SOUTH);
            F.setVisible(true);
        }
    }

    static class gestionFenetre extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
}

但它给了我两个粘贴的拆分窗格:-/

你能帮帮我吗?我希望你能理解我的信息,因为我学英语。如果您想要此问题的下一阶段,请在下面与我联系,谢谢! ^^ 这里是实际的图形测试java类:

package sample;

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


class Fenetresaisie extends JFrame {

    public static class Fenetre {
        public final static int HT = 1024;
        public final static int LG = 758;

        public static void main(String[] args) {
            JPanel panel = new JPanel();
            JFrame F = new JFrame("CORONAZE");
            F.setExtendedState(JFrame.MAXIMIZED_BOTH);
            F.setSize(HT, LG);
            F.setVisible(true);

            F.addWindowListener(new gestionFenetre());

            ImageIcon icone = new ImageIcon("images.jpg");

            JLabel image = new JLabel(icone);

            JTextField textField = new JTextField();
            textField.setFont(new Font("Terminal", Font.BOLD, 30));
            textField.setForeground(Color.RED);
            textField.setBackground(Color.black);

            textField.addKeyListener(new java.awt.event.KeyAdapter() {

                public void keyReleased(java.awt.event.KeyEvent e) {
                     textField.getText();
                     e.getKeyChar();
                }

            });



            JLabel label = new JLabel(">texte de l'histoire ici<");
            label.setOpaque(true);
            label.setForeground(Color.green);
            label.setBackground(Color.BLACK);
            panel.add(label);

            JSplitPane topJSplitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, label, textField);
            topJSplitPane.setDividerLocation(400);


           // JSplitPane bottomJSplitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, topJSplitPane, textField );
            //i added it to have a double separation, but it give 2 sticked splitpane
            F.add(topJSplitPane, BorderLayout.CENTER);
           // F.add(bottomJSplitPane, BorderLayout.SOUTH);
            F.setVisible(true);
        }
    }

    static class gestionFenetre extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
}

【问题讨论】:

  • 一个问题是您将一个JSplitPane 添加到另一个中(这是正确的),然后又将两者都添加到您的JFrame 中(这是错误的)。因此,只添加包含另一个的 SplitPane -> F.add(bottomJSplitPane)。这可能会解决您的问题。如果没有,请查看this question,这正是您的用例。
  • 另外,你的类Fenetresaisie不需要扩展JFrame,如果你使用默认的JFrame,那么无论如何。
  • 1) 我猜挂起的编辑已经这样做了,但是将来如果您发布链接,请使用 [Text to display](link url) 格式。 2)使用Java Naming Conventions,你的内部类gestionFenetre应该以大写字母开头,这是类名的标准。

标签: java swing jframe jsplitpane


【解决方案1】:

您在问题中写道

我是一名新的 Java 程序员,从一开始就使用 stackoverflow

我真的认为学习Swing编程的方法是遵循从基础开始逐渐进步的学习曲线。每个人都有自己喜欢的学习方式,例如参加课程、观看视频或阅读书籍。个人比较喜欢书。如果你也这样做,那么我可以推荐一些。

你也写了你的问题

我编写了一个小“游戏”

我会说这对于初学者来说是一个非常雄心勃勃的项目。虽然我确信有些人从雄心勃勃的项目开始学习效果最好,但我想说他们是少数。

也就是说,正确实现 GUI 的关键是深入了解 Swing 的工作原理,尤其是 layout managers 和 Component 的大小以及代码中的什么位置可以你设置那些Component 大小。

下面的代码最初将显示您想要的 GUI,因为我从您的问题中了解到,这就是您现在想要完成的。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class WindowCapture extends WindowAdapter implements Runnable {
    private JFrame  frame;
    private JLabel  label;
    private JSplitPane  splitPane;
    private JSplitPane  topPane;

    @Override // java.lang.Runnable
    public void run() {
        showGui();
    }

    @Override // java.awt.event.WindowAdapter
    public void windowOpened(WindowEvent event) {
        int height = event.getWindow().getHeight();
        splitPane.setDividerLocation(0.7);
        double high = height * 0.7;
        height = (int) Math.rint(high);
        high = height * 0.8;
        height = (int) Math.rint(high);
        label.setPreferredSize(new Dimension(event.getWindow().getWidth(), height));
    }

    private JTextField createBottomPane() {
        JTextField textField = new JTextField(20);
        textField.setFont(new Font("Terminal", Font.BOLD, 30));
        textField.setForeground(Color.RED);
        textField.setBackground(Color.black);
        return textField;
    }

    private JSplitPane createSplitPane() {
        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createTopPane(), createBottomPane());
        splitPane.setDividerLocation(0.4);
        return splitPane;
    }

    private JSplitPane createTopPane() {
        label = new JLabel(">texte de l'histoire ici<");
        label.setOpaque(true);
        label.setForeground(Color.green);
        label.setBackground(Color.BLACK);
        topPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                            label,
                                            new JPanel());
        topPane.setDividerLocation(0.9);
        return topPane;
    }

    public void showGui() {
        frame = new JFrame("Window Capture");
        frame.addWindowListener(this);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.add(createSplitPane());
        frame.setVisible(true);
    }

    /**
     * Start here!
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new WindowCapture());
    }
}

这是正在运行的应用程序的屏幕截图。

【讨论】:

  • ...哇...首先:非常感谢您回答我的问题!和...这就是我想要的:-) 顺便说一句:我通过许多教程、书籍和论坛学习编码。我编写代码是因为它是一个“自学教程”:我发现了“项目”的这个想法,我必须完成它。代码的每一部分都是一个新概念来介绍!这不是一个严肃的项目;-)!我发现这部分代码非常有趣!这又是很多新概念,但我以后会研究它!再次感谢您的提示和代码!论坛万岁 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多