问题描述
我找不到如何解决代码中错误的解决方案。任何建议都会有所帮助。
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));
}
}
IntelijIdea说
“ getContentPane()。add(weightTabbedPane,gridConstraints);”上的错误
但我不知道如何解决。 IntelijIdea提供了创建private
方法的选项,但是还有其他方法可以解决此问题。任何建议都会有所帮助。
解决方法
扩展JFrame或JPanel时无需获取getContentPane()
!您已经在容器中。只需调用add将一个组件添加到容器中即可。
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));
}
}
,
JPanel继承了java.awt.Container类,因此JPanel类本身就是一个容器。
没有实现getContentPane()方法。
替换行
getContentPane().add(weightTabbedPane,gridConstraints);
作者
add(weightTabbedPane,gridConstraints);