嗨,我已经在一个简单的GUI中途,似乎无法弄清楚为什么我的drawLines等没有绘制到页面上。我想在添加滑块之前在DrawingPanel上绘制相交线,以调整线相交的位置,但似乎无法显示它们。我是java的新手,代码不是很完整,但任何帮助都将不胜感激!感谢
package JPanel;
import java.awt.*;
import javax.swing.*;
public class JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("GUI Design");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
BoxLayout layout = new BoxLayout (frame.getContentPane(), BoxLayout.X_AXIS);
frame.setLayout(layout);
JPanel DrawingPanel = new JPanel();
JPanel ControlPanel = new JPanel();
ControlPanel.setBackground (Color.WHITE);
ControlPanel.setPreferredSize (new Dimension (200, 500));
DrawingPanel.setPreferredSize (new Dimension (500, 500));
frame.setSize(700,500);
JLabel label1 = new JLabel ("Quadrant");
JLabel label2 = new JLabel ("X: " + x + ", Y: " + y);
double x=0;
double y=0;
JSlider slide = new JSlider();
slide.setOrientation(SwingConstants.HORIZONTAL);
slide.setMinimum(1);
slide.setMaximum(4);
ControlPanel.add (label1);
ControlPanel.add (slide);
ControlPanel.add (label2);
frame.getContentPane().add(DrawingPanel);
frame.getContentPane().add(ControlPanel);
frame.pack();
frame.setVisible(true);
}
}
package DrawingPanel;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawingPanel extends JPanel {
public void paint (Graphics page) {
super.paintComponent (page);
page.drawLine(10, 10, 180, 300);
//I have tried messing around with this but can't get it to draw.
//super.paintComponent also returns a 'cannot find symbol' error
//which i am confused about. Also the code is imcomplete
}
}