移动滚动条时,jpanel变得混乱

时间:2018-01-06 11:35:33

标签: java user-interface windowsbuilder

在我的一个项目中,我想在Jpanel中画线,它的工作正常,直到我移动滚动它变得凌乱。我不知道如何刷新屏幕来删除这个凌乱的绘图,以及他们来自哪里! 谢谢。

这是我的代码:

public class MainGui {

private JFrame frmGcodeplotter;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainGui window = new MainGui();
                window.frmGcodeplotter.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainGui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmGcodeplotter = new JFrame();
    frmGcodeplotter.setTitle("GcodePlotter");
    frmGcodeplotter.setResizable(false);
    frmGcodeplotter.setBounds(100, 100, 942, 697);
    frmGcodeplotter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmGcodeplotter.getContentPane().setLayout(null);


    try {
        javax.swing.UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Label label = new Label("Gcode Pannel");
    label.setBounds(10, 128, 103, 24);
    frmGcodeplotter.getContentPane().add(label);

    Label label_1 = new Label("X:");
    label_1.setBounds(10, 500, 25, 24);
    frmGcodeplotter.getContentPane().add(label_1);

    Label label_2 = new Label("Y:");
    label_2.setBounds(10, 530, 25, 24);
    frmGcodeplotter.getContentPane().add(label_2);

    Label label_3 = new Label("Z:");
    label_3.setBounds(10, 560, 25, 24);
    frmGcodeplotter.getContentPane().add(label_3);

    Label label_4 = new Label("Phi:");
    label_4.setBounds(10, 590, 25, 24);
    frmGcodeplotter.getContentPane().add(label_4);

    Label Xlabel = new Label("0.000 mm");
    Xlabel.setBounds(41, 500, 70, 24);
    frmGcodeplotter.getContentPane().add(Xlabel);

    Label YLabel = new Label("0.000 mm");
    YLabel.setBounds(41, 530, 70, 24);
    frmGcodeplotter.getContentPane().add(YLabel);

    Label Zlabel = new Label("0.000 mm");
    Zlabel.setBounds(41, 560, 70, 24);
    frmGcodeplotter.getContentPane().add(Zlabel);

    Label PhiLabel = new Label("0.000 mm");
    PhiLabel.setBounds(41, 590, 70, 24);
    frmGcodeplotter.getContentPane().add(PhiLabel);

    TextArea textArea = new TextArea();
    textArea.setBounds(10, 158, 203, 336);
    frmGcodeplotter.getContentPane().add(textArea);

    JButton btnStart = new JButton("Start");
    btnStart.setBounds(10, 13, 97, 25);
    frmGcodeplotter.getContentPane().add(btnStart);

    JButton btnPause = new JButton("Pause");
    btnPause.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnPause.setBounds(10, 51, 97, 25);
    frmGcodeplotter.getContentPane().add(btnPause);

    JButton btnStop = new JButton("Stop");
    btnStop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnStop.setBounds(10, 89, 97, 25);
    frmGcodeplotter.getContentPane().add(btnStop);

    JButton btnSend = new JButton("Send");
    btnSend.setBounds(116, 13, 97, 25);
    frmGcodeplotter.getContentPane().add(btnSend);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(225, 13, 699, 610);
    frmGcodeplotter.getContentPane().add(scrollPane);

//  JPanel panel = new JPanel(); //new Plotter(1000,1000);
    Plotter plotter = new Plotter(1000,1000);
    plotter.setForeground(Color.BLACK);
    plotter.setBackground(Color.WHITE);
    scrollPane.setViewportView(plotter);
    GroupLayout gl_plotter = new GroupLayout(plotter);
    gl_plotter.setHorizontalGroup(
        gl_plotter.createParallelGroup(Alignment.LEADING)
            .addGap(0, 1000, Short.MAX_VALUE)
    );
    gl_plotter.setVerticalGroup(
        gl_plotter.createParallelGroup(Alignment.LEADING)
            .addGap(0, 1000, Short.MAX_VALUE)
    );
    plotter.setLayout(gl_plotter);

    JButton btnConnect = new JButton("Connect");
    btnConnect.setBounds(116, 51, 97, 25);
    frmGcodeplotter.getContentPane().add(btnConnect);

    JMenuBar menuBar = new JMenuBar();
    frmGcodeplotter.setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmOpen = new JMenuItem("Open");
    mnFile.add(mntmOpen);

    JMenuItem mntmSave = new JMenuItem("Save");
    mnFile.add(mntmSave);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mnFile.add(mntmExit);

    JMenu mnEdit = new JMenu("Edit");
    menuBar.add(mnEdit);

    JMenu mnCoonection = new JMenu("Coonection");
    menuBar.add(mnCoonection);

    JMenuItem mntmSerial = new JMenuItem("Serial");
    mnCoonection.add(mntmSerial);

    JMenu mnAbout = new JMenu("About");
    menuBar.add(mnAbout);

    JMenuItem mntmContactUs = new JMenuItem("Contact Us");
    mnAbout.add(mntmContactUs);
}

}

这是绘图仪:

public class Plotter extends JPanel {


  /**
 * 
 */
private static final long serialVersionUID = -5142922023476436317L;
private int lines;
    private int lineGap;

    public Plotter(int lines, int lineGap)
    {
        // super();
        this.lines = lines;
        this.lineGap = lineGap;
    }

    @Override
    public Dimension getPreferredSize()
    {

        return new Dimension(lines, lineGap);
    }

    @Override
    public void paintComponent(Graphics g)
    {
       int width = getWidth();
       int height = getHeight();
       g.drawRect(0, 0, 200, 200);

        System.out.println("Width:"+width+" Height:"+ height);

    }     

}

这是我移动滚动时jpanel的屏幕截图 moved scroll jpanel

1 个答案:

答案 0 :(得分:0)

Finnaly我遇到了问题。 在paint组件中,它必须称为“super.paintComponent(g)”。 目前的代码是:

 public class Plotter extends JPanel {


  /**
 * 
 */

private static final long serialVersionUID = -5142922023476436317L;
private int lines;
    private int lineGap;

public Plotter(int lines, int lineGap)
{
    // super();
    this.lines = lines;
    this.lineGap = lineGap;
}

@Override
public Dimension getPreferredSize()
{

    return new Dimension(lines, lineGap);
}

@Override
public void paintComponent(Graphics g)
{
   super.paintComponent(g);
   int width = getWidth();
   int height = getHeight();
   g.drawRect(0, 0, 200, 200);

    System.out.println("Width:"+width+" Height:"+ height);

}     
}