Java中JInternalFrame中JTabbedPane上JPanel的尺寸设置

发布于 2025-01-03 20:12:05 字数 7613 浏览 7 评论 0原文

我的软件的用户需要能够单击不同的选项卡来查看不同类型的数据表示形式。但是,当用户单击选项卡时,我在下面包含的代码不会显示请求的数据面板。

您可以通过运行下面的代码轻松地重新创建问题,然后在代码将生成的 GUI 中执行以下步骤:

1.) Select "New" from the file menu    
2.) Click on "AnotherTab" in the internal frame which will appear

根据您在下面注释掉的代码行,该选项卡将只显示一个空白面板,或者将在面板顶部的中间显示一个小红色方块。

您可以切换/注释掉以重现此问题的代码行是:

GraphPanel myGP = new GraphPanel();
//GraphPanel myGP = new GraphPanel(width,height);

这些代码行位于下面的 GraphGUI.java 中。

谁能告诉我如何修复下面的代码,以便 myGP 以包含它的面板的完整尺寸显示?

以下是重新创建此问题所需的三个 java 文件:

ParentFrame.java

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;

public class ParentFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JLayeredPane desktop;
JInternalFrame internalFrame;

public ParentFrame() {
    super("Parent Frame");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(800, 400));
    Panel p = new Panel();
    this.add(p, BorderLayout.SOUTH);
    desktop = new JDesktopPane();
    setJMenuBar(createMenuBar());
    this.add(desktop, BorderLayout.CENTER);
    this.pack();
    this.setSize(new Dimension(800, 600));
    this.setLocationRelativeTo(null);
}
protected JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    //Set up the File menu.
    JMenu FileMenu = new JMenu("File");
    FileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(FileMenu);
    //Set up the first menu item.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(new OpenListener());
    FileMenu.add(menuItem);
    //Set up the second menu item.
    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    FileMenu.add(menuItem);

    return menuBar;
    }
class OpenListener implements ActionListener {
    private static final int DELTA = 40;
    private int offset = DELTA;
    public void actionPerformed(ActionEvent e) {
        // create internal frame
        int ifWidth = 600;
        int ifHeight = 300;
        internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
        internalFrame.setLocation(offset, offset);
        offset += DELTA;

        // create jtabbed pane
        JTabbedPane jtp = createTabbedPane();
        internalFrame.add(jtp);
        desktop.add(internalFrame);
        internalFrame.pack();
        internalFrame.setSize(new Dimension(ifWidth,ifHeight));
        internalFrame.setVisible(true);
    }
}
private JTabbedPane createTabbedPane() {
    JTabbedPane jtp = new JTabbedPane();
    jtp.setMinimumSize(new Dimension(600,300));
    createTab(jtp, "One Tab");
    createTab(jtp, "AnotherTab");
    createTab(jtp, "Tab #3");
    return jtp;
}
private void createTab(JTabbedPane jtp, String s) {
    if(s=="AnotherTab"){
        jtp.getHeight();
        jtp.getWidth();
        GraphGUI myGraphGUI = new GraphGUI(jtp.getHeight(),jtp.getWidth());
        jtp.add(s, myGraphGUI);
    }
    else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
}
public static void main(String args[]) {
    ParentFrame myParentFrame = new ParentFrame();
    myParentFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {if ("quit".equals(e.getActionCommand())){System.exit(0);}}
}

GraphGUI.java:您可以在其中切换注释以重新创建问题。

import javax.swing.*;

class GraphGUI extends JPanel{
GraphGUI(int height,int width) {
    //REPRODUCE ERROR BY COMMENTING OUT EITHER ONE OF NEXT TWO LINES:
    GraphPanel myGP = new GraphPanel();
//      GraphPanel myGP = new GraphPanel(width,height);
    this.add(myGP);
    this.setVisible(true);// Display the panel.
}
}

GraphPanel.java:

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

class GraphPanel extends JPanel {
Insets ins; // holds the panel's insets
double[] plotData;
double xScale;

GraphPanel(int w, int h) {
    setOpaque(true);// Ensure that panel is opaque.
    setPreferredSize(new Dimension(w, h));// Set preferred dimension as specfied.
    setMinimumSize(new Dimension(w, h));// Set preferred dimension as specfied.
    setMaximumSize(new Dimension(w, h));// Set preferred dimension as specfied.
}
GraphPanel() {
    setOpaque(true);// Ensure that panel is opaque.
}

protected void paintComponent(Graphics g){// Override paintComponent() method.
    super.paintComponent(g);// Always call superclass method first.
    int height = getHeight();// Get height of component.
    int width = getWidth();// Get width of component.
    System.out.println("height, width are: "+height+" , "+width);
    ins = getInsets();// Get the insets.
    // Get dimensions of text
    Graphics2D g2d = (Graphics2D)g;
    FontMetrics fontMetrics = g2d.getFontMetrics();
    String xString = ("x-axis label");
    int xStrWidth = fontMetrics.stringWidth(xString);
    int xStrHeight = fontMetrics.getHeight();
    String yString = "y-axis label";
    int yStrWidth = fontMetrics.stringWidth(yString);
    int yStrHeight = fontMetrics.getHeight();
    String titleString ="Title of Graphic";
    int titleStrWidth = fontMetrics.stringWidth(titleString);
    int titleStrHeight = fontMetrics.getHeight();
    //get margins
    int leftMargin = ins.left;
    //set parameters for inner rectangle
    int hPad=10;
    int vPad = 6;
    int numYticks = 10;
    int testLeftStartPlotWindow = ins.left+5+(3*yStrHeight);
    int testInnerWidth = width-testLeftStartPlotWindow-ins.right-hPad;
    int remainder = testInnerWidth%numYticks;
    int leftStartPlotWindow = testLeftStartPlotWindow-remainder;
    System.out.println("remainder is: "+remainder);
    int blueWidth = testInnerWidth-remainder;
    int blueTop = ins.bottom+(vPad/2)+titleStrHeight;
    int bottomPad = (3*xStrHeight)-vPad;
    int blueHeight = height-bottomPad-blueTop;

    g.setColor(Color.red);
    int redWidth = width-leftMargin-1;
    //plot outer rectangle
    g.drawRect(leftMargin, ins.bottom, redWidth, height-ins.bottom-1);
    System.out.println("blueWidth is: "+blueWidth);
    // fill inner rectangle
    g.setColor(Color.white);
    g.fillRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight);

    //write top label
    g.setColor(Color.black);
    g.drawString(titleString, (width/2)-(titleStrWidth/2), titleStrHeight);

    //write x-axis label
    g.setColor(Color.red);
    g.drawString(xString, (width/2)-(xStrWidth/2), height-ins.bottom-vPad);
    g2d.rotate(Math.toRadians(-90), 0, 0);//rotate text 90 degrees counter-clockwise
    //write y-axis label
    g.drawString(yString, -(height/2)-(yStrWidth/2), yStrHeight);
    g2d.rotate(Math.toRadians(+90), 0, 0);//rotate text 90 degrees clockwise
    // plot inner rectangle
    g.setColor(Color.blue);
    g.drawRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight);
}
}

Users of my software need to be able to click on different tabs to see different types of data representations. However, the code I am including below does not show the requested data panel when the user clicks on a tab.

You can re-create the problem easily by running the code below, and then following these steps in the GUI which the code will produce:

1.) Select "New" from the file menu    
2.) Click on "AnotherTab" in the internal frame which will appear

Depending on which line of code you comment out below, the tab will either just show a blank panel or will show a tiny red square in the middle of the top of the panel.

The lines of code that you can toggle/comment-out to recreate this problem are:

GraphPanel myGP = new GraphPanel();
//GraphPanel myGP = new GraphPanel(width,height);

These lines of code are in GraphGUI.java below.

Can anyone show me how to fix the code below so that myGP gets displayed at the full size of the panel containing it?

Here are the three java files required to recreate this problem:

ParentFrame.java

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;

public class ParentFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JLayeredPane desktop;
JInternalFrame internalFrame;

public ParentFrame() {
    super("Parent Frame");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(800, 400));
    Panel p = new Panel();
    this.add(p, BorderLayout.SOUTH);
    desktop = new JDesktopPane();
    setJMenuBar(createMenuBar());
    this.add(desktop, BorderLayout.CENTER);
    this.pack();
    this.setSize(new Dimension(800, 600));
    this.setLocationRelativeTo(null);
}
protected JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    //Set up the File menu.
    JMenu FileMenu = new JMenu("File");
    FileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(FileMenu);
    //Set up the first menu item.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(new OpenListener());
    FileMenu.add(menuItem);
    //Set up the second menu item.
    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    FileMenu.add(menuItem);

    return menuBar;
    }
class OpenListener implements ActionListener {
    private static final int DELTA = 40;
    private int offset = DELTA;
    public void actionPerformed(ActionEvent e) {
        // create internal frame
        int ifWidth = 600;
        int ifHeight = 300;
        internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
        internalFrame.setLocation(offset, offset);
        offset += DELTA;

        // create jtabbed pane
        JTabbedPane jtp = createTabbedPane();
        internalFrame.add(jtp);
        desktop.add(internalFrame);
        internalFrame.pack();
        internalFrame.setSize(new Dimension(ifWidth,ifHeight));
        internalFrame.setVisible(true);
    }
}
private JTabbedPane createTabbedPane() {
    JTabbedPane jtp = new JTabbedPane();
    jtp.setMinimumSize(new Dimension(600,300));
    createTab(jtp, "One Tab");
    createTab(jtp, "AnotherTab");
    createTab(jtp, "Tab #3");
    return jtp;
}
private void createTab(JTabbedPane jtp, String s) {
    if(s=="AnotherTab"){
        jtp.getHeight();
        jtp.getWidth();
        GraphGUI myGraphGUI = new GraphGUI(jtp.getHeight(),jtp.getWidth());
        jtp.add(s, myGraphGUI);
    }
    else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
}
public static void main(String args[]) {
    ParentFrame myParentFrame = new ParentFrame();
    myParentFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {if ("quit".equals(e.getActionCommand())){System.exit(0);}}
}

GraphGUI.java: This is the one where you can toggle comments to re-create the problem.

import javax.swing.*;

class GraphGUI extends JPanel{
GraphGUI(int height,int width) {
    //REPRODUCE ERROR BY COMMENTING OUT EITHER ONE OF NEXT TWO LINES:
    GraphPanel myGP = new GraphPanel();
//      GraphPanel myGP = new GraphPanel(width,height);
    this.add(myGP);
    this.setVisible(true);// Display the panel.
}
}

GraphPanel.java:

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

class GraphPanel extends JPanel {
Insets ins; // holds the panel's insets
double[] plotData;
double xScale;

GraphPanel(int w, int h) {
    setOpaque(true);// Ensure that panel is opaque.
    setPreferredSize(new Dimension(w, h));// Set preferred dimension as specfied.
    setMinimumSize(new Dimension(w, h));// Set preferred dimension as specfied.
    setMaximumSize(new Dimension(w, h));// Set preferred dimension as specfied.
}
GraphPanel() {
    setOpaque(true);// Ensure that panel is opaque.
}

protected void paintComponent(Graphics g){// Override paintComponent() method.
    super.paintComponent(g);// Always call superclass method first.
    int height = getHeight();// Get height of component.
    int width = getWidth();// Get width of component.
    System.out.println("height, width are: "+height+" , "+width);
    ins = getInsets();// Get the insets.
    // Get dimensions of text
    Graphics2D g2d = (Graphics2D)g;
    FontMetrics fontMetrics = g2d.getFontMetrics();
    String xString = ("x-axis label");
    int xStrWidth = fontMetrics.stringWidth(xString);
    int xStrHeight = fontMetrics.getHeight();
    String yString = "y-axis label";
    int yStrWidth = fontMetrics.stringWidth(yString);
    int yStrHeight = fontMetrics.getHeight();
    String titleString ="Title of Graphic";
    int titleStrWidth = fontMetrics.stringWidth(titleString);
    int titleStrHeight = fontMetrics.getHeight();
    //get margins
    int leftMargin = ins.left;
    //set parameters for inner rectangle
    int hPad=10;
    int vPad = 6;
    int numYticks = 10;
    int testLeftStartPlotWindow = ins.left+5+(3*yStrHeight);
    int testInnerWidth = width-testLeftStartPlotWindow-ins.right-hPad;
    int remainder = testInnerWidth%numYticks;
    int leftStartPlotWindow = testLeftStartPlotWindow-remainder;
    System.out.println("remainder is: "+remainder);
    int blueWidth = testInnerWidth-remainder;
    int blueTop = ins.bottom+(vPad/2)+titleStrHeight;
    int bottomPad = (3*xStrHeight)-vPad;
    int blueHeight = height-bottomPad-blueTop;

    g.setColor(Color.red);
    int redWidth = width-leftMargin-1;
    //plot outer rectangle
    g.drawRect(leftMargin, ins.bottom, redWidth, height-ins.bottom-1);
    System.out.println("blueWidth is: "+blueWidth);
    // fill inner rectangle
    g.setColor(Color.white);
    g.fillRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight);

    //write top label
    g.setColor(Color.black);
    g.drawString(titleString, (width/2)-(titleStrWidth/2), titleStrHeight);

    //write x-axis label
    g.setColor(Color.red);
    g.drawString(xString, (width/2)-(xStrWidth/2), height-ins.bottom-vPad);
    g2d.rotate(Math.toRadians(-90), 0, 0);//rotate text 90 degrees counter-clockwise
    //write y-axis label
    g.drawString(yString, -(height/2)-(yStrWidth/2), yStrHeight);
    g2d.rotate(Math.toRadians(+90), 0, 0);//rotate text 90 degrees clockwise
    // plot inner rectangle
    g.setColor(Color.blue);
    g.drawRect(leftStartPlotWindow, blueTop, blueWidth, blueHeight);
}
}

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

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

发布评论

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

评论(1

长不大的小祸害 2025-01-10 20:12:05
class GraphGUI extends JPanel {

    .
    .
    GraphGUI(int height,int width) {
    // components in a GridLayout are stretched to fit space available
    setLayout(new GridLayout());
class GraphGUI extends JPanel {

    .
    .
    GraphGUI(int height,int width) {
    // components in a GridLayout are stretched to fit space available
    setLayout(new GridLayout());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文