将两个垂直的 JSplitPane 放在一个框架上 java

put two vertical JSplitPane on a frame java

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

基本上,它是一个双分离,有 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 class:

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);
        }
    }
}

您在问题中写道

I am a new java programmer and I use Whosebug since my begin

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

你的问题也写了

I code a little "game"

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

也就是说,正确实现 GUI 的关键是深入了解 Swing 的工作原理,尤其是 layout managers and 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());
    }
}

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