how2j.cn

下载区
文件名 文件大小
j.png 437b
pics.rar 65k
本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频



13分36秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器



示例 1 : 基本面板   
示例 2 : ContentPane   
示例 3 : SplitPanel   
示例 4 : JScrollPanel   
示例 5 : TabbedPanel   
示例 6 : CardLayerout   
示例 7 : 练习-SplitPanel   
示例 8 : 答案-SplitPanel   
示例 9 : 练习-按照eclipse的风格显示多个java文件   
示例 10 : 答案-按照eclipse的风格显示多个java文件   

JPanel即为基本面板
面板和JFrame一样都是容器,不过面板一般用来充当中间容器,把组件放在面板上,然后再把面板放在窗体上。
一旦移动一个面板,其上面的组件,就会全部统一跟着移动,采用这种方式,便于进行整体界面的设计
基本面板
package gui; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 200); f.setLayout(null); JPanel p1 = new JPanel(); // 设置面板大小 p1.setBounds(50, 50, 300, 60); // 设置面板背景颜色 p1.setBackground(Color.RED); // 这一句可以没有,因为JPanel默认就是采用的FlowLayout p1.setLayout(new FlowLayout()); JButton b1 = new JButton("英雄1"); JButton b2 = new JButton("英雄2"); JButton b3 = new JButton("英雄3"); // 把按钮加入面板 p1.add(b1); p1.add(b2); p1.add(b3); JPanel p2 = new JPanel(); JButton b4 = new JButton("英雄4"); JButton b5 = new JButton("英雄5"); JButton b6 = new JButton("英雄6"); p2.add(b4); p2.add(b5); p2.add(b6); p2.setBackground(Color.BLUE); p2.setBounds(10, 150, 300, 60); // 把面板加入窗口 f.add(p1); f.add(p2); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
JFrame上有一层面板,叫做ContentPane
平时通过f.add()向JFrame增加组件,其实是向JFrame上的 ContentPane加东西
package gui; import javax.swing.JButton; import javax.swing.JFrame; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 200); f.setLayout(null); JButton b = new JButton("一键秒对方基地挂"); b.setBounds(50, 50, 280, 30); f.add(b); // JFrame上有一层面板,叫做ContentPane // 平时通过f.add()向JFrame增加组件,其实是向JFrame上的 ContentPane加东西 // f.add等同于f.getContentPane().add(b); f.getContentPane().add(b); // b.getParent()获取按钮b所处于的容器 // 打印出来可以看到,实际上是ContentPane而非JFrame System.out.println(b.getParent()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
创建一个水平JSplitPane,左边是pLeft,右边是pRight
SplitPanel
package gui; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSplitPane; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 200); f.setLayout(null); JPanel pLeft = new JPanel(); pLeft.setBounds(50, 50, 300, 60); pLeft.setBackground(Color.RED); pLeft.setLayout(new FlowLayout()); JButton b1 = new JButton("盖伦"); JButton b2 = new JButton("提莫"); JButton b3 = new JButton("安妮"); pLeft.add(b1); pLeft.add(b2); pLeft.add(b3); JPanel pRight = new JPanel(); JButton b4 = new JButton("英雄4"); JButton b5 = new JButton("英雄5"); JButton b6 = new JButton("英雄6"); pRight.add(b4); pRight.add(b5); pRight.add(b6); pRight.setBackground(Color.BLUE); pRight.setBounds(10, 150, 300, 60); // 创建一个水平JSplitPane,左边是p1,右边是p2 JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pLeft, pRight); // 设置分割条的位置 sp.setDividerLocation(80); // 把sp当作ContentPane f.setContentPane(sp); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
使用带滚动条的面板有两种方式
1. 在创建JScrollPane,把组件作为参数传进去

JScrollPane sp = new JScrollPane(ta);

2. 希望带滚动条的面板显示其他组件的时候,调用setViewportView

sp.setViewportView(ta);
JScrollPanel
package gui; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 200); f.setLayout(null); //准备一个文本域,在里面放很多数据 JTextArea ta = new JTextArea(); for (int i = 0; i < 1000; i++) { ta.append(String.valueOf(i)); } //自动换行 ta.setLineWrap(true); JScrollPane sp = new JScrollPane(ta); f.setContentPane(sp); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
TabbedPanel
package gui; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 200); f.setLayout(null); JPanel p1 = new JPanel(); p1.setBounds(50, 50, 300, 60); p1.setBackground(Color.RED); p1.setLayout(new FlowLayout()); JButton b1 = new JButton("英雄1"); JButton b2 = new JButton("英雄2"); JButton b3 = new JButton("英雄3"); p1.add(b1); p1.add(b2); p1.add(b3); JPanel p2 = new JPanel(); JButton b4 = new JButton("英雄4"); JButton b5 = new JButton("英雄5"); JButton b6 = new JButton("英雄6"); p2.add(b4); p2.add(b5); p2.add(b6); p2.setBackground(Color.BLUE); p2.setBounds(10, 150, 300, 60); JTabbedPane tp = new JTabbedPane(); tp.add(p1); tp.add(p2); // 设置tab的标题 tp.setTitleAt(0, "红色tab"); tp.setTitleAt(1, "蓝色tab"); ImageIcon i = new ImageIcon("e:/project/j2se/j.png"); tp.setIconAt(0,i ); tp.setIconAt(1,i ); f.setContentPane(tp); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
CardLayerout 布局器 很像TabbedPanel ,在本例里面上面是一个下拉框,下面是一个CardLayerout 的JPanel
这个JPanel里有两个面板,可以通过CardLayerout方便的切换
CardLayerout
package gui; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("CardLayerout"); JPanel comboBoxPane = new JPanel(); String buttonPanel = "按钮面板"; String inputPanel = "输入框面板"; String comboBoxItems[] = { buttonPanel, inputPanel }; JComboBox<String> cb = new JComboBox<>(comboBoxItems); comboBoxPane.add(cb); // 两个Panel充当卡片 JPanel card1 = new JPanel(); card1.add(new JButton("按钮 1")); card1.add(new JButton("按钮 2")); card1.add(new JButton("按钮 3")); JPanel card2 = new JPanel(); card2.add(new JTextField("输入框", 20)); JPanel cards; // a panel that uses CardLayout cards = new JPanel(new CardLayout()); cards.add(card1, buttonPanel); cards.add(card2, inputPanel); f.add(comboBoxPane, BorderLayout.NORTH); f.add(cards, BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(250, 150); f.setLocationRelativeTo(null); f.setVisible(true); cb.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent evt) { CardLayout cl = (CardLayout) (cards.getLayout()); cl.show(cards, (String) evt.getItem()); } }); } }
示例 7 :

练习-SplitPanel

edit  姿势不对,事倍功半! 点击查看做练习的正确姿势
设计一个像SplitPanel的左右风格的SplitPanel
左边放3个按钮,上面的文字分别是: 盖伦,提莫,安妮
右边默认显示盖伦
当左边按钮点击的时候,右边就会显示对应的图片

需要的头像可以下载区 下载pics.rar
练习-SplitPanel
示例 8 :

答案-SplitPanel

edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费3个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
查看本答案会花费3个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频

2分28秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器


package gui; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(400, 300); f.setLocation(200, 200); f.setLayout(null); JPanel pLeft = new JPanel(); pLeft.setBounds(50, 50, 300, 60); pLeft.setBackground(Color.LIGHT_GRAY); pLeft.setLayout(new FlowLayout()); JButton b1 = new JButton("盖伦"); JButton b2 = new JButton("提莫"); JButton b3 = new JButton("安妮"); pLeft.add(b1); pLeft.add(b2); pLeft.add(b3); JPanel pRight = new JPanel(); JLabel lPic = new JLabel(""); ImageIcon i = new ImageIcon("e:/project/j2se/gareen.jpg"); lPic.setIcon(i); pRight.add(lPic); pRight.setBackground(Color.lightGray); pRight.setBounds(10, 150, 300, 60); // 创建一个水平JSplitPane,左边是p1,右边是p2 JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pLeft, pRight); // 设置分割条的位置 sp.setDividerLocation(80); // 把sp当作ContentPane f.setContentPane(sp); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); switchPic(b1,"gareen",lPic); switchPic(b2,"teemo",lPic); switchPic(b3,"annie",lPic); } private static void switchPic(JButton b1, String fileName, JLabel lPic) { b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ImageIcon i = new ImageIcon("e:/project/j2se/"+fileName+".jpg"); lPic.setIcon(i); } }); } }
示例 9 :

练习-按照eclipse的风格显示多个java文件

edit  姿势不对,事倍功半! 点击查看做练习的正确姿势
参考eclipse的风格,借助TabbedPanel显示一个包下所有的java文件
假设包 jdbc 中有如下java文件
ConnectionPool.java
DAO.java
HeroDAO.java
TestConnectionPool.java
TestDAO.java
TestJDBC.java
练习-按照eclipse的风格显示多个java文件
示例 10 :

答案-按照eclipse的风格显示多个java文件

edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费5个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
查看本答案会花费5个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频

10分8秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器


首先准备一个JavaFilePane 专门用于显示文件内容的Panel

然后在TestGUI中遍历e:/project/j2se/jdbc 下的文件,并根据这些文件生成JavaFilePane 。

接着把这些JavaFilePane 插入到TabbedPanel中即可
package gui; import java.awt.BorderLayout; import java.io.File; import java.io.FileReader; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class JavaFilePane extends JPanel{ public JavaFilePane(File file){ this.setLayout(new BorderLayout()); String fileContent = getFileContent(file); JTextArea ta = new JTextArea(); ta.setText(fileContent); this.add(ta); } private String getFileContent(File f){ String fileContent = null; try (FileReader fr = new FileReader(f)) { char[] all = new char[(int) f.length()]; fr.read(all); fileContent= new String(all); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileContent; } public static void main(String[] args) { JFrame f =new JFrame(); f.setSize(400,300); f.setContentPane(new JavaFilePane(new File("E:/project/j2se/src/gui/JavaFilePane.java"))); f.setVisible(true); } }
package gui; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JTabbedPane; public class TestGUI { public static void main(String[] args) { JFrame f = new JFrame("LoL"); f.setSize(800, 600); f.setLocationRelativeTo(null); f.setLayout(null); File folder = new File("E:/project/j2se/src/jdbc"); File[] fs=folder.listFiles(); JTabbedPane tp = new JTabbedPane(); ImageIcon icon = new ImageIcon("e:/project/j2se/j.png"); for (int i = 0; i < fs.length; i++) { JavaFilePane jfp =new JavaFilePane(fs[i]); tp.add(jfp); tp.setIconAt(i,icon ); tp.setTitleAt(i, shortName(fs[i].getName())); } f.setContentPane(tp); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } private static String shortName(String name) { int length = 6; if(name.length()>length){ return name.substring(0,length) + "..."; } return name; } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2023-09-03 练习-按照eclipse的风格显示多个java文件
旧时亭台阁




通过 JScrollPane 和 JTabbedPane 实现。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Objects;

public class TestGUI {
    public static void main(String[] args) throws IOException {
        // 窗体
        JFrame frame = new JFrame("Eclipse");
        frame.setLocation(1000, 500);
        frame.setSize(800, 600);

        // 主面板
        JTabbedPane tabbedPane = new JTabbedPane();
        frame.setContentPane(tabbedPane);

        // 获取 jdbc 包下的 java 文件
        File[] fileArr = new File("src/jdbc").listFiles(f -> f.getName().endsWith(".java"));
        for (int i = 0; i < Objects.requireNonNull(fileArr).length; i++) {
            File file = fileArr[i];

            // 每个 java 文件,创建一个面板
            JScrollPane scrollPane = new JScrollPane();
            tabbedPane.add(scrollPane);

            // 面板对应的 tab
            tabbedPane.setTitleAt(i, file.getName());
            tabbedPane.setIconAt(i, new ImageIcon("assets/img/j.png"));


            // 面板,添加文本域
            JTextArea textArea = new JTextArea();
            textArea.setLineWrap(true);
            scrollPane.setViewportView(textArea);

            // 文本域,填充文本
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while (null != (line = reader.readLine())) {
                textArea.append(line);
                textArea.append("\r\n");
            }
            reader.close();
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

							





回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2023-09-03 练习 - SplitPanel
旧时亭台阁




给按钮添加事件响应
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

public class TestGUI {
    public static void main(String[] args) {
        // 窗体
        JFrame frame = new JFrame("LOL");
        frame.setLocation(1000, 500);
        frame.setSize(500, 400);

        // 主面板
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        JSplitPane splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        splitPanel.setDividerLocation(80);
        frame.setContentPane(splitPanel);

        // 业务配置
        Map<String, String> config = new HashMap<>();
        config.put("盖伦", "assets/img/gareen.jpg");
        config.put("提莫", "assets/img/teemo.jpg");
        config.put("安妮", "assets/img/annie.jpg");

        // 右边面板
        JLabel label = new JLabel();
        rightPanel.add(label);

        // 左边面板
        config.forEach((k, v) -> {
            JButton button = new JButton(k);
            leftPanel.add(button);

            // 按钮事件
            button.addActionListener(e -> {
                ImageIcon icon = new ImageIcon(v);
                label.setIcon(icon);
            });
        });
        // 默认点击第一个按钮
        JButton firstBtn = (JButton) leftPanel.getComponent(0);
        firstBtn.doClick();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

							


1 个答案

CharlieLong
答案时间:2023-10-09
设计一个像SplitPanel的左右风格的SplitPanel 左边放3个按钮,上面的文字分别是: 盖伦,提莫,安妮 右边默认显示盖伦 当左边按钮点击的时候,右边就会显示对应的图片



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2021-11-23 练习-按照eclipse的风格显示多个java文件
2021-11-23 练习 - SplitPanel
2021-11-11 为什么报错禁止访问


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 37 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 中级-图形界面-面板 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图