任何关于如何修复和获取WeightPlot和WeightEditor选项卡的建议?

huangapple go评论71阅读模式
英文:

Any suggestions how to fix and get WeightPlot and WeightEditor tabs?

问题

以下是您要求的翻译部分:

无法找到解决代码中错误的方法。任何建议都会有帮助。

package weightmonitor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;

public class WeightMonitor extends JFrame {

    // 菜单
    JMenuBar mainMenuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem newMenuItem = new JMenuItem("New Weight File");
    JMenuItem openMenuItem = new JMenuItem("Open Weight File");
    JMenuItem saveMenuItem = new JMenuItem("Save Weight File");
    JMenuItem exitMenuItem = new JMenuItem("Exit");

    //

    public static void main(String args[]) {
        // 创建框架
        new WeightMonitor().setVisible(true);
    }

    public WeightMonitor() {
        // 框架构造函数
        setTitle("Weight Monitor");
        setResizable(true);

        setJMenuBar(mainMenuBar);
        mainMenuBar.add(fileMenu);
        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.addSeparator();
        fileMenu.add(exitMenuItem);

        newMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                newMenuItemActionPerformed(e);
            }
        });

        openMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openMenuItemActionPerformed(e);
            }
        });

        saveMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveMenuItemActionPerformed(e);
            }
        });

        exitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exitMenuItemActionPerformed(e);
            }
        });

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt) {
                exitForm(evt);
            }
        });

        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gridConstraints;

        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int)(0.5 * (screenSize.width - getWidth())),
            (int)(0.5 * (screenSize.height - getHeight())),
            getWidth(), getHeight());
    }

    private void newMenuItemActionPerformed(ActionEvent e) {}
    private void openMenuItemActionPerformed(ActionEvent e) {}
    private void saveMenuItemActionPerformed(ActionEvent e) {}
    private void exitMenuItemActionPerformed(ActionEvent e) {}

    private void exitForm(WindowEvent evt) {
        System.exit(0);
    }
}

class WeightPlotPanel extends JPanel {
    // 标签控制和面板的声明
    JTabbedPane weightTabbedPane = new JTabbedPane();
    JPanel editorPanel = new JPanel();
    WeightPlotPanel plotPanel = new WeightPlotPanel();

    public void paintComponent(Graphics g) {
        Rectangle2D.Double plotFrame;
        Graphics2D g2D = (Graphics2D) g;
        super.paintComponent(g2D);

        // 绘制图框
        plotFrame = new Rectangle2D.Double(50, 40, 420, 280);
        g2D.setPaint(Color.WHITE);
        g2D.fill(plotFrame);
        g2D.setStroke(new BasicStroke(2));
        g2D.setPaint(Color.BLACK);
        g2D.draw(plotFrame);

        g2D.dispose();
        //pack();

        weightTabbedPane.setPreferredSize(new Dimension(500, 400));
        weightTabbedPane.addTab("Weight Editor", editorPanel);
        weightTabbedPane.addTab("Weight Plot", plotPanel);
        GridBagConstraints gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        getContentPane().add(weightTabbedPane, gridConstraints);

        editorPanel.setBackground(new Color(192, 192, 255));
        editorPanel.setLayout(new GridBagLayout());
        plotPanel.setBackground(new Color(255, 192, 192));
    }
}

IntelliJ IDEA 提示

在 "getContentPane().add(weightTabbedPane, gridConstraints);" 上存在错误。

但我不知道如何修复它。IntelliJ IDEA 提供了创建私有方法的选项,但是否有其他方法来解决这个问题呢?任何建议都会有帮助。

英文:

I can't find the solution on how to fix Errors in the code. Any advice would be helpful.

package weightmonitor;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
public class WeightMonitor extends JFrame {
//menu
JMenuBar mainMenuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newMenuItem = new JMenuItem("New Weight File");
JMenuItem openMenuItem = new JMenuItem("Open Weight  File");
JMenuItem saveMenuItem = new JMenuItem("Save Weight  File");
JMenuItem exitMenuItem = new JMenuItem("Exit");
//
public static void main(String args[]) {
//create frame
new WeightMonitor().setVisible(true);
}
public WeightMonitor() {
//frame constructor
setTitle("Weight Monitor");
setResizable(true);
setJMenuBar(mainMenuBar);
mainMenuBar.add(fileMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
newMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newMenuItemActionPerformed(e);
}
});
openMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openMenuItemActionPerformed(e);
}
});
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveMenuItemActionPerformed(e);
}
});
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exitMenuItemActionPerformed(e);
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gridConstraints;
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int)(0.5 * (screenSize.width - getWidth())),
(int)(0.5 * (screenSize.height - getHeight())),
getWidth(), getHeight());
}
private void newMenuItemActionPerformed(ActionEvent e) {}
private void openMenuItemActionPerformed(ActionEvent e) {}
private void saveMenuItemActionPerformed(ActionEvent e) {}
private void exitMenuItemActionPerformed(ActionEvent e) {}
private void exitForm(WindowEvent evt) {
System.exit(0);
}
}
class WeightPlotPanel extends JPanel {
//Declaration of tab control and panels
JTabbedPane weightTabbedPane = new JTabbedPane();
JPanel editorPanel = new JPanel();
WeightPlotPanel plotPanel = new WeightPlotPanel();
public void paintComponent(Graphics g) {
Rectangle2D.Double plotFrame;
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
//draw plot frame
plotFrame = new Rectangle2D.Double(50, 40, 420, 280);
g2D.setPaint(Color.WHITE);
g2D.fill(plotFrame);
g2D.setStroke(new BasicStroke(2));
g2D.setPaint(Color.BLACK);
g2D.draw(plotFrame);
g2D.dispose();
//pack();
weightTabbedPane.setPreferredSize(new Dimension(500, 400));
weightTabbedPane.addTab("Weight Editor", editorPanel);
weightTabbedPane.addTab("Weight Plot", plotPanel);
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
getContentPane().add(weightTabbedPane, gridConstraints);
editorPanel.setBackground(new Color(192, 192, 255));
editorPanel.setLayout(new GridBagLayout());
plotPanel.setBackground(new Color(255, 192, 192));
}
}

IntelijIdea says

> Error on "getContentPane().add(weightTabbedPane, gridConstraints);"

but I don't know how to fix it. The IntelijIdea gives the option to create a private method but is there any other way to fix this. Any advice would be helpful.

答案1

得分: 1

当您扩展JFrame或JPanel时,不需要获取getContentPane()!您已经在一个容器中。只需调用add将组件添加到容器中。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;

public class WeightMonitor extends JFrame {

    // 菜单
    JMenuBar mainMenuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem newMenuItem = new JMenuItem("New Weight File");
    JMenuItem openMenuItem = new JMenuItem("Open Weight  File");
    JMenuItem saveMenuItem = new JMenuItem("Save Weight  File");
    JMenuItem exitMenuItem = new JMenuItem("Exit");

    // 其他部分...

    public WeightMonitor() {
        // 构造函数
        setTitle("Weight Monitor");
        setResizable(true);

        setJMenuBar(mainMenuBar);
        mainMenuBar.add(fileMenu);
        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.addSeparator();
        fileMenu.add(exitMenuItem);

        // 其他部分...
        
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gridConstraints;

        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int) (0.5 * (screenSize.width - getWidth())),
                (int) (0.5 * (screenSize.height - getHeight())),
                getWidth(), getHeight());
    }

    // 其他部分...
}

class WeightPlotPanel extends JPanel {
    // 其他部分...

    public void paintComponent(Graphics g) {
        Rectangle2D.Double plotFrame;
        Graphics2D g2D = (Graphics2D) g;
        super.paintComponent(g2D);

        // 绘制绘图框架
        plotFrame = new Rectangle2D.Double(50, 40, 420, 280);
        g2D.setPaint(Color.WHITE);
        g2D.fill(plotFrame);
        g2D.setStroke(new BasicStroke(2));
        g2D.setPaint(Color.BLACK);
        g2D.draw(plotFrame);

        g2D.dispose();

        weightTabbedPane.setPreferredSize(new Dimension(500,  400));
        weightTabbedPane.addTab("Weight Editor", editorPanel);
        weightTabbedPane.addTab("Weight Plot", plotPanel);
        GridBagConstraints gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        add(weightTabbedPane, gridConstraints);

        editorPanel.setBackground(new Color(192, 192, 255));
        editorPanel.setLayout(new GridBagLayout());
        plotPanel.setBackground(new Color(255, 192, 192));
    }
}
英文:

No need to get getContentPane() When you extend JFrame or JPanel! You are already in a container. Just call add to add a component to the container.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
public class WeightMonitor extends JFrame {
//menu
JMenuBar mainMenuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newMenuItem = new JMenuItem("New Weight File");
JMenuItem openMenuItem = new JMenuItem("Open Weight  File");
JMenuItem saveMenuItem = new JMenuItem("Save Weight  File");
JMenuItem exitMenuItem = new JMenuItem("Exit");
//
public static void main(String args[]){
//create frame
new WeightMonitor().setVisible(true);
}
public WeightMonitor(){
//frame constructor
setTitle("Weight Monitor");
setResizable(true);
setJMenuBar(mainMenuBar);
mainMenuBar.add(fileMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
newMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)  {
newMenuItemActionPerformed(e);  }
});
openMenuItem.addActionListener(new ActionListener()  {
public void actionPerformed(ActionEvent e)  {
openMenuItemActionPerformed(e);  }  });
saveMenuItem.addActionListener(new ActionListener()  {
public void actionPerformed(ActionEvent e)  {
saveMenuItemActionPerformed(e);  }  });
exitMenuItem.addActionListener(new ActionListener()  {
public void actionPerformed(ActionEvent e)  {
exitMenuItemActionPerformed(e);  }  });
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gridConstraints;
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) (0.5 * (screenSize.width - getWidth())),
(int) (0.5 * (screenSize.height - getHeight())),
getWidth(), getHeight());
}
private void newMenuItemActionPerformed(ActionEvent e)  {  }
private void openMenuItemActionPerformed(ActionEvent e)  {  }
private void saveMenuItemActionPerformed(ActionEvent e)  {  }
private void exitMenuItemActionPerformed(ActionEvent e)  {  }
private void exitForm(WindowEvent evt){
System.exit(0);
}
}
class WeightPlotPanel extends JPanel
{
//Declaration of tab control and panels
JTabbedPane weightTabbedPane = new JTabbedPane();
JPanel editorPanel = new JPanel();
WeightPlotPanel plotPanel = new WeightPlotPanel();
public void paintComponent(Graphics g)
{
Rectangle2D.Double plotFrame;
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
//draw plot frame
plotFrame = new Rectangle2D.Double(50, 40, 420, 280);
g2D.setPaint(Color.WHITE);
g2D.fill(plotFrame);
g2D.setStroke(new BasicStroke(2));
g2D.setPaint(Color.BLACK);
g2D.draw(plotFrame);
g2D.dispose();
//pack();
weightTabbedPane.setPreferredSize(new Dimension(500,  400));
weightTabbedPane.addTab("Weight Editor", editorPanel);
weightTabbedPane.addTab("Weight Plot", plotPanel);
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
add(weightTabbedPane, gridConstraints);
editorPanel.setBackground(new Color(192, 192, 255));
editorPanel.setLayout(new GridBagLayout());
plotPanel.setBackground(new Color(255, 192, 192));
}
}

答案2

得分: 0

JPanel继承自类java.awt.Container,因此JPanel类本身就是一个容器。

方法getContentPane()没有实现。

将以下行进行替换:

getContentPane().add(weightTabbedPane, gridConstraints);

改为

add(weightTabbedPane, gridConstraints);
英文:

JPanel inherit the class java.awt.Container, so the class JPanel is a container itself.

There is no implementation of the method getContentPane().

Replace the line

getContentPane().add(weightTabbedPane, gridConstraints);

by

add(weightTabbedPane, gridConstraints);

huangapple
  • 本文由 发表于 2020年9月20日 14:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63976095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定