JScrollPane 没有显示?

发布于 2024-09-27 05:19:20 字数 4261 浏览 4 评论 0原文

我的十六进制编辑器上有一些 JScrollPanes,但它们没有显示。 知道为什么吗?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class HexEditor extends JFrame{
    JScrollPane hexScroll;
    JScrollPane byteScroll;
    JTextArea hexArea;
    JTextArea byteArea;
    JFileChooser chooser;// = new JFileChooser();
    FileInputStream fin;
    JMenuBar menuBar;
    JMenu file;
        JMenuItem load;

    public HexEditor(){
        super("Cypri's java hex editor");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setResizable(false);

        chooser = new JFileChooser();

        load = new JMenuItem("Load");
            load.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event) {

                    try{

                        openFile();
                        fin = new FileInputStream(chooser.getSelectedFile());

                        int ch;
                        StringBuffer strContent = new StringBuffer("");

                        for(int i = 0; (ch = fin.read()) != -1; i++){
                            String s = Integer.toHexString(ch);

                            if(s.length() < 2)
                                s = "0" + Integer.toHexString(ch);

                            if(i < 10)
                                strContent.append(" " + s.toUpperCase());

                            else{
                                strContent.append(" " + s.toUpperCase() + "\n");
                                i = 0;
                            }
                        }

                        hexArea.setText(strContent.toString());

                        byte[] b = hexStringToByteArray(strContent.toString());
                        char[] chars = new char[b.length];
                        String byteText = "";
                        int newLine = 0;
                        for(int i = 0; i < b.length; i++){
                            chars[i] = (char) b[i];
                            byteText += chars[i];

                            newLine++;
                            if(newLine > 10){
                                byteText += "\n";
                                newLine = 0;
                            }
                        }

                        hexArea.setText(strContent.toString());
                        byteArea.setText(byteText);
                        packMe();

                        //textArea.setWrapStyleWord(true);
                        //textArea.setColumns(50);
                        //textArea.setRows(50);
                    }

                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            });

        file = new JMenu("File");
        file.add(load);

        menuBar = new JMenuBar();
        menuBar.add(file);

        hexArea = new JTextArea();
        byteArea = new JTextArea();

        hexScroll = new JScrollPane();
        byteScroll = new JScrollPane();

        hexScroll.add(hexArea);
        hexScroll.setSize(500, 480);
        byteScroll.add(byteArea);
        byteScroll.setSize(500, 480);

        setSize(640, 480);
        //getContentPane().setBackground(Color.);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(BorderLayout.NORTH, menuBar);
        getContentPane().add(BorderLayout.WEST, hexScroll);
        getContentPane().add(BorderLayout.EAST, byteScroll);
        pack();
        setVisible(true);
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length() -1;
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }


    public void openFile(){
        chooser.showOpenDialog(null);
    }

    public void packMe(){
        pack();
    }

    public static void main(String[] args){
        HexEditor app = new HexEditor();
    }
}

I have some JScrollPanes on my hex editor and they're not showing up.
Any idea why?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class HexEditor extends JFrame{
    JScrollPane hexScroll;
    JScrollPane byteScroll;
    JTextArea hexArea;
    JTextArea byteArea;
    JFileChooser chooser;// = new JFileChooser();
    FileInputStream fin;
    JMenuBar menuBar;
    JMenu file;
        JMenuItem load;

    public HexEditor(){
        super("Cypri's java hex editor");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setResizable(false);

        chooser = new JFileChooser();

        load = new JMenuItem("Load");
            load.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event) {

                    try{

                        openFile();
                        fin = new FileInputStream(chooser.getSelectedFile());

                        int ch;
                        StringBuffer strContent = new StringBuffer("");

                        for(int i = 0; (ch = fin.read()) != -1; i++){
                            String s = Integer.toHexString(ch);

                            if(s.length() < 2)
                                s = "0" + Integer.toHexString(ch);

                            if(i < 10)
                                strContent.append(" " + s.toUpperCase());

                            else{
                                strContent.append(" " + s.toUpperCase() + "\n");
                                i = 0;
                            }
                        }

                        hexArea.setText(strContent.toString());

                        byte[] b = hexStringToByteArray(strContent.toString());
                        char[] chars = new char[b.length];
                        String byteText = "";
                        int newLine = 0;
                        for(int i = 0; i < b.length; i++){
                            chars[i] = (char) b[i];
                            byteText += chars[i];

                            newLine++;
                            if(newLine > 10){
                                byteText += "\n";
                                newLine = 0;
                            }
                        }

                        hexArea.setText(strContent.toString());
                        byteArea.setText(byteText);
                        packMe();

                        //textArea.setWrapStyleWord(true);
                        //textArea.setColumns(50);
                        //textArea.setRows(50);
                    }

                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            });

        file = new JMenu("File");
        file.add(load);

        menuBar = new JMenuBar();
        menuBar.add(file);

        hexArea = new JTextArea();
        byteArea = new JTextArea();

        hexScroll = new JScrollPane();
        byteScroll = new JScrollPane();

        hexScroll.add(hexArea);
        hexScroll.setSize(500, 480);
        byteScroll.add(byteArea);
        byteScroll.setSize(500, 480);

        setSize(640, 480);
        //getContentPane().setBackground(Color.);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(BorderLayout.NORTH, menuBar);
        getContentPane().add(BorderLayout.WEST, hexScroll);
        getContentPane().add(BorderLayout.EAST, byteScroll);
        pack();
        setVisible(true);
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length() -1;
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }


    public void openFile(){
        chooser.showOpenDialog(null);
    }

    public void packMe(){
        pack();
    }

    public static void main(String[] args){
        HexEditor app = new HexEditor();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヅ她的身影、若隐若现 2024-10-04 05:19:20

您的滚动窗格就在那里,但默认行为是仅在需要时显示滚动条。要强制显示它们,您需要进行以下更改:

hexScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
byteScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

由于 BorderLayout 的行为,您的滚动窗格非常窄。

Your scrollpanes are there, but the default behavior is to only show the scrollbars when needed. To force them to be shown, you make these changes:

hexScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
byteScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

Your scrollpanes are very narrow due to the behavior of your BorderLayout.

在风中等你 2024-10-04 05:19:20

只有 BorderLayout 的中心才会被“填充”。您需要更改为其他布局(例如 GridBagLayout)并使用weightx、weighty 和 fill 使它们更加可见,或者嵌套您的 BorderLayout。但它们就在那里,你只是看不到它们,因为它们很小:)

我建议查看 GridBagLayout 教程

Only the CENTER of a BorderLayout will be 'filled'. You need to either change to another layout like GridBagLayout and use weightx, weighty and fill to make them more visible or nest your BorderLayouts. But they are there, you just cannot see them as they are small :)

I'd recommend looking at a tutorial for GridBagLayout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文