【问题标题】:How to draw a vertical line in JPanel at a specific position?如何在JPanel中的特定位置绘制一条垂直线?
【发布时间】:2013-02-01 15:13:53
【问题描述】:

我正在尝试使用JPanel 为这个媒体播放应用程序设计一个时间线。为了提供当前视频帧相对于时间线的指示,我需要能够在扩展JPanel 的这个TimeLinePanel 类中的特定x 坐标处绘制一条垂直线。到目前为止,我在使用paint() 函数或覆盖paintComponent() 时运气不佳。当我尝试在我的NewJApplet(此类扩展JApplet)类中使用paint() 函数时,绘制了垂直线,但是我在时间轴上使用的JPanel 消失了(边框没有更长的可见性)。此外,我听说使用paint() 方法并不可取。结果,我尝试在JPanel 中覆盖paintComponent() 方法,并尝试从NewJApplet 调用paintComponent 方法,但它没有用垂直线更新timeLinePanel1

任何帮助将不胜感激。

以下是我目前的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helloworld;

import java.awt.GridLayout;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Justin
 */
public class NewJApplet extends javax.swing.JApplet {

    /**
     * Initializes the applet NewJApplet
     */
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */

        try {
            /*
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
            * */
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        //</editor-fold>

        /* Create and display the applet */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                    timeLinePanel1.paintComponent(timeLinePanel1.getGraphics());
                    timeLinePanel1.repaint();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        myPanel = new javax.swing.JPanel();
        timeLinePanel1 = new helloworld.TimeLinePanel();

        myPanel.setBackground(new java.awt.Color(51, 51, 51));

        javax.swing.GroupLayout myPanelLayout = new javax.swing.GroupLayout(myPanel);
        myPanel.setLayout(myPanelLayout);
        myPanelLayout.setHorizontalGroup(
            myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(myPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(timeLinePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
                .addContainerGap())
        );
        myPanelLayout.setVerticalGroup(
            myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, myPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(timeLinePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(myPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(myPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JPanel myPanel;
    private helloworld.TimeLinePanel timeLinePanel1;
    // End of variables declaration
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helloworld;

import java.awt.GridLayout;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Justin
 */
public class TimeLinePanel extends javax.swing.JPanel {

    /**
     * Creates new form TimeLinePanel
     */
    public TimeLinePanel() {
        initComponents();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Rectangle r = new Rectangle(3, 0, 1, 25);
        g.fillRect(25, 0, 1, 25);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
}

这是我尝试运行小程序时得到的。理想情况下,我想在白框(timeLinePanel1)中的某个(x,y)坐标处绘制一条垂直线,例如(25,0)(如代码中所示)以了解如何我可以继续在 JPanel 中绘制垂直线。

【问题讨论】:

  • 其他组件会出现在时间线上吗?如果没有,使用BufferedImage 作为画布会更简单,并将其显示在标签上。我猜您看到的很多问题都与首选尺寸和布局有关。使用图片应该可以解决这些问题。

标签: java swing jpanel paintcomponent japplet


【解决方案1】:

您需要调用 super.paintComponent 来绘制面板中的所有内容:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillRect(25, 0, 1, 25);
}

【讨论】:

    【解决方案2】:

    我不确定,这是正确的答案,但您可能忘记设置颜色?

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        Rectangle r = new Rectangle(3, 0, 1, 25);
        g.fillRect(25, 0, 1, 25);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多