2014-01-24 103 views
0

我试图绘制一些酒吧,有随机整数到Java小程序波纹管,但覆盖有一个错误即时通讯新的Java我试图makr代码中的两个领域绘制方法是在最底部另一个是第二面板区域 什么IM瞄准绘制图形(g)到jpanel

http://i.stack.imgur.com/oRdi4.jpg

尺寸X,Y坐标不

- 全尺寸= 200,250

-location GUI = 200,50

-image = 140150

-precip = 100,20

-temp = 20100个

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.GridBagLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
import javax.swing.*; 
import java.util.*; 

public class Final2 extends JFrame 
{ 
    //Panels 
    JPanel locationGui = new JPanel(); 
    JPanel temp = new JPanel(); 
    JPanel percip = new JPanel(); 
    JPanel image = new JPanel(); 
    //Location gui components 
    JButton Enter, Exit; 
    JTextField location; 
    JLabel city; 
    JRadioButton time; 
    JComboBox Seasons; 
    //bar # genertor 
    Random rand = new Random(); 
    int P = rand.nextInt (100) + 1; //Random Precipitation 
    int H = rand.nextInt (50) + 1; //Random Heat 
    public Final2() 
    { 
     init(); 

    } 


    public void init() 
    { 
     Font font = new Font ("impact", Font.PLAIN, 20); 
     //________________________________________________new panel____________________ 
     locationGui.setBackground (Color.RED); 
     JLabel guiLabel = new JLabel (""); 
     guiLabel.setFont (font); 

     Enter = new JButton ("Enter"); 
     Exit = new JButton ("exit"); 



     city = new JLabel ("What city?"); 
     location = new JTextField (20); //location entry field 


     Seasons = new JComboBox(); 
     Seasons.addItem ("Summer"); 
     Seasons.addItem ("Fall"); 
     Seasons.addItem ("Winter"); 
     Seasons.addItem ("Spring"); 


     time = new JRadioButton ("check if night?"); 


     locationGui.add (city); 
     locationGui.add (location); 
     locationGui.add (Seasons); 
     locationGui.add (time); 



     locationGui.add (guiLabel); 


     //________________________________________________new panel____________________ 
     temp.setBackground (Color.BLUE); 
     temp.setLayout (new GridBagLayout()); 
     JLabel tempLabel = new JLabel ("Temp"); 
     tempLabel.setFont (font); 
     temp.add (tempLabel); 

     //________________________________________________new panel____________________ 
     percip.setBackground (Color.GREEN); 
     JLabel pLabel = new DrawingPanel(); //where it should be 

     percip.add (pLabel); 

     //________________________________________________new panel____________________ 
     image.setBackground (Color.ORANGE); 
     image.setLayout (new GridBagLayout()); 
     JLabel imageLabel = new JLabel ("Image"); 
     imageLabel.setFont (font); 
     image.add (imageLabel); 

     Container contentPane = getContentPane(); 

     contentPane.setLayout (new BorderLayout()); 
     contentPane.add (locationGui, BorderLayout.NORTH); 
     contentPane.add (temp, BorderLayout.EAST); 
     contentPane.add (percip, BorderLayout.SOUTH); 
     contentPane.add (image, BorderLayout.CENTER); 

     setContentPane (contentPane); 
     setDefaultCloseOperation (EXIT_ON_CLOSE); 
     setSize (400, 400); 
     setLocationRelativeTo (null); 
     setVisible (true); 
    } 


    public static void main (String[] args) 
    { 
     SwingUtilities.invokeLater (new Runnable() 

     { 


      public void run() 
      { 
       new Final2(); 
      } 
     } 


     ); 
    } 


    private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm 
    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     drawPercip(g); 
    } 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 400); 
    } 



    } 


    public void drawPercip (Graphics g) 
    { 
     //Precipitation Bar 
     g.setColor (Color.black); 
     g.drawRect (40, 170, 100, 20); //outline of bar 
     g.setColor (Color.blue); 
     g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar) 
    } 


    public void drawTemp (Graphics g) 
    { 
     //Temparature Bar 
     g.setColor (Color.red); 
     g.fillRect (170 + 4, 50, 14, 100); //Covers in 
     g.setColor (Color.black); 
     g.drawRect (170, 50, 20, 100); //outline of bar 
     g.setColor (Color.white); 
     g.fillRect (170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar) 
    } 




} 

我的当前错误(4)在@override正在发生在这个代码块提供的唯一消息是“非法令牌”和“意外符号被忽略”idk为什么不提供更多信息

private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm 
    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     drawPercip(g); 
    } 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 400); 
    } 
+0

读取时间如果出现错误,你应该张贴他们。请注意,'paintComponent'应该受到保护,但这不应该成为问题。 –

+0

即时业余爱好者如此idk什么保护手段和错误是“非法令牌”和“意想不到的符号被忽略”即时通讯使用准备Java作为编译器 –

+0

1)不要套用您的错误消息。而是将整个邮件作为对您问题的修改发布。在您的代码中指明发生错误的位置。 –

回答

2

首先,我看到没有小程序,

其次,DrawingPanel不是JLabel

JLabel pLabel = new DrawingPanel(); //where it should be 

更改pLable类型JComponentJPanelDrawingPanel这取决于你希望能够访问了每个类的东西。

第三,你不应该依赖于幻数

public void drawPercip(Graphics g) { 
    //Precipitation Bar 
    g.setColor(Color.black); 
    g.drawRect(40, 170, 100, 20); //outline of bar 
    g.setColor(Color.blue); 
    g.fillRect(40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar) 
} 

public void drawTemp(Graphics g) { 
    //Temparature Bar 
    g.setColor(Color.red); 
    g.fillRect(170 + 4, 50, 14, 100); //Covers in 
    g.setColor(Color.black); 
    g.drawRect(170, 50, 20, 100); //outline of bar 
    g.setColor(Color.white); 
    g.fillRect(170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar) 
} 

不保证该面板的高度和宽度是什么,你认为这可能是。使用的getWidthgetHeight方法

第四,如果你想有更多的一个DrawingPanel,你应该让

int P = rand.nextInt(100) + 1; //Random Precipitation 
int H = rand.nextInt(50) + 1; //Random Heat 

部分的DrawingPanel的。

您还应该通过Code Conventions for the Java Programming Language

+0

我不太明白你在说什么,但我增加了更多信息旋转压轴布局的想法和尺寸即时消息后,为每个项目 –

+0

什么部分你不明白? – MadProgrammer

+0

1)你说要改变Plalab到DrawingPanel,它已经是 2)我从来没有听说过“幻数” –