2013-07-22 48 views
4

我试图画我的JFrame,但我无法让我的super.paintComponents(g);工作。另外,当我在paintComponent()方法中告诉它时,我的JFrame上没有画任何东西。为什么我无法在我的JFrame上“绘制”?

下面是代码:

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

public class MTGSAMPServerReference extends JFrame implements ActionListener { 

    public static Toolkit tk = Toolkit.getDefaultToolkit(); 
    static int ScrnWidth = ((int) tk.getScreenSize().getWidth()); 
    static int ScrnHeight = ((int) tk.getScreenSize().getHeight()); 
    private static final long serialVersionUID = 1L; 
    private static JList list1; 
    private static JButton next; 

    public MTGSAMPServerReference() { 
     // set flow layout for the frame 
     this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING)); 
     Object[]mainData = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP"}; 
     JPanel controls = new JPanel(new BorderLayout(5,5)); 
     list1 = new JList<Object>(mainData); 
     list1.setVisibleRowCount(10); 
     next = new JButton("Next"); 
     next.addActionListener(this); 
     controls.add(new JScrollPane(list1)); 
     controls.add(next, BorderLayout.PAGE_END); 
     controls.setBorder(new EmptyBorder(25,25,0,0)); 
     add(controls); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getActionCommand().equals("Next")) { 
      int index = list1.getSelectedIndex(); 
      System.out.println("Index Selected: " + index); 
      String s = (String) list1.getSelectedValue(); 
      System.out.println("Value Selected: " + s); 
     } 
    } 

    public void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame f = new MTGSAMPServerReference(); 
     //Display the window. 
     f.pack(); 
     f.setVisible(true); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(1200, 800); 
     f.setLocationRelativeTo(null); 
     list1.setSize(250, 250); 
     list1.setLocation(0, 0); 
     next.setSize(75, 25); 
     next.setLocation(251, 276); 
     MTGSAMPServerReference.this.repaint(); 
    } 

    protected void paintComponent(Graphics g) { 
     //super.paintComponent(g); << Can't seem to get this to work. 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.drawRect(0, 0, 50, 50); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
      MTGSAMPServerReference gui = new MTGSAMPServerReference(); 
      gui.createAndShowGUI(); 
      } 
     }); 
    } 
} 

我以前跟paintComponent()工作,但似乎仍不能找出我做错了。我知道它必须是一个简单的解决方法,但不能为我的生活发现它。有任何想法吗?

任何和所有的帮助表示赞赏。

在此先感谢!

回答

4

在您的paintComponent方法上使用@Override注释令人惊讶。这就是为什么使用这个注释是很有用的,因为它会在编译时标记你,如果你不认为你应该这么做,那么你不会重写一个方法。

解决方案:出于多种原因,从未在JFrame中“绘制”过。而是按照教程要求做的事情 - 在JPanel或JComponent的paintComponent(...)方法中绘制。如果你搜索这个网站,你会发现我们在这里告诉过许多人同样的事情,实际上我建议你这样做。如果这个问题是重复的,我不会感到惊讶,因为这是一个相当普遍的问题。

注意这不会 “工作”(实际上不会编译):

super.paintComponent(g); << Can't seem to get this to work. 

出于同样的原因 - 有没有super.paintComponent(g)为一个JFrame。

此外,关于,

我与的paintComponent()工作过,但似乎仍然无法找出什么我做错了。

但是,如果你看看你以前的代码,你会发现这个方法从来没有在JFrame中直接使用过,也不应该这样做。

1

paintComponent()JPanel类的成员,而不是您尝试调用它的JFrame类。

这就是为什么你无法拨打super.paintComponent(Graphics g)。编译器认为你正在创建自己完全不相关的方法,该方法也恰好被称为paintComponent()

创建一个继承JPanel的类,然后在其中复制并贴上您的paintComponent()方法。

像气垫船Full Of Eels评论,你可以通过直接在方法头上添加@Override标签来检查是否正确覆盖了方法;如果你收到错误,你做错了什么。

有关JPanelJFrame的更多信息,请参阅我对this question的回答。

+0

哎呦傻我 – scottyseus

+0

修正了那个错误 – scottyseus

相关问题