2012-11-29 70 views
3

我有一个小问题画图的JPanel - 我该如何重绘的JPanel主类从其他类/文件/方法?的Java:从不同类别

故事是,我有一个Swing窗口,其中有一个方法,这是由该文件夹中的文件,通过和类去,和阅读方法及其说明(注释)自动生成列表。现在,问题出现了,当一个方法需要绘制一个方法时,让我们说,使用圆和线的太阳,那么我怎样才能让它显示在主窗口中?

我看过(阅读:谷歌搜索)周围,并发现JPanel应该是方便的,但从另一个类和方法绘制它,而不知道什么是他们的名字和他们什么时候使用绘画,创建。困难。

我也加入了主要的方法,如果想有什么用处..

static int indez = 0; 
    protected static JList list; 
    static boolean valitud = false; 
    public static JTextArea txtala; 
    public static JScrollPane scrl; 
    public static JPanel pilt; 
    static Map<Integer, String[]> info = new TreeMap<Integer, String[]>(); 
    static ArrayList<Integer> nrs = new ArrayList<Integer>(); 
    public static void main(String[] args){ 
     Iterator<Entry<Integer, String[]>> iterator; 
     Entry<Integer, String[]> entry; 
     Integer val = 0; 

     JFrame f = new JFrame("Praktikumid"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(750, 350); 

     JPanel panel = new JPanel(); 
     pilt = new JPanel(){public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawString("This is my custom Panel!", 10, 20); 
     }}; 

     pilt.setVisible(false); 
     pilt.setPreferredSize(new Dimension(f.getWidth() - 200, f.getHeight() - 20)); 
     pilt.setBackground(Color.GRAY); 

     GroupLayout layout = new GroupLayout(panel); 
     panel.setLayout(layout); 
     layout.setAutoCreateGaps(true); 
     layout.setAutoCreateContainerGaps(true); 

     ActionListener kuular = new ActionListener() { 
      public void actionPerformed(ActionEvent tegu) { 
       valitud = true; 
       try { 
        praxStarter(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 

     MouseListener hiir = new MouseListener() { 
      public void mouseClicked(MouseEvent e) { 
       indez = list.getSelectedIndex(); 
      } 
      @Override 
      public void mouseEntered(MouseEvent e) {} 
      @Override 
      public void mouseExited(MouseEvent e) {} 
      @Override 
      public void mousePressed(MouseEvent e) {} 
      @Override 
      public void mouseReleased(MouseEvent e) {} 
     }; 

     JLabel kiri = new JLabel("Vali hiirega ja vajuta nupule"); 

     ArrayList<String> selections = new ArrayList<String>(); 

     // Iterating over all the items in another method to get the modules 

     list = new JList(selections.toArray()); 
     list.setSelectedIndex(0); 
     list.addMouseListener(hiir); 

     JScrollPane listScroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     listScroller.setMinimumSize(new Dimension(200, 50)); 
     listScroller.setMaximumSize(new Dimension(200, f.getHeight() * 1000)); 

     JButton button = new JButton("Vali"); 
     button.setActionCommand(Integer.toString(list.getSelectedIndex())); 
     button.addActionListener(kuular); 

     Font font = new Font("Arial", Font.PLAIN, 12); 
     txtala = new JTextArea("<-- Vali sealt"); 
     txtala.setEditable(false); 
     txtala.setMargin(new Insets(5, 8, 0, 0)); 
     txtala.setFont(font); 
     scrl = new JScrollPane(txtala, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     scrl.setPreferredSize(new Dimension(f.getWidth() - 200, f.getHeight() - 20)); 

     pilt.setBackground(Color.PINK); 
     pilt.setVisible(false); 
     pilt.setPreferredSize(new Dimension(f.getWidth() - 200, f.getHeight() - 20)); 

     GroupLayout.ParallelGroup menu = layout.createParallelGroup(); 
     GroupLayout.SequentialGroup row = layout.createSequentialGroup(); 
     GroupLayout.SequentialGroup leftright = layout.createSequentialGroup(); 
     GroupLayout.ParallelGroup topdown = layout.createParallelGroup(); 
     GroupLayout.ParallelGroup dbl = layout.createParallelGroup(); 
     GroupLayout.ParallelGroup dblrow = layout.createParallelGroup(); 

     menu.addComponent(kiri); 
     menu.addComponent(listScroller); 
     menu.addComponent(button); 
     leftright.addGroup(menu); 
     dbl.addComponent(scrl); 
     dbl.addComponent(pilt); 
     leftright.addGroup(dbl); 

     row.addComponent(kiri); 
     row.addComponent(listScroller); 
     row.addComponent(button); 
     topdown.addGroup(row); 
     dblrow.addComponent(scrl); 
     dblrow.addComponent(pilt); 
     topdown.addGroup(dblrow); 

     layout.setHorizontalGroup(leftright); 
     layout.setVerticalGroup(topdown); 

     panel.setComponentZOrder(pilt, 1); 
     panel.setComponentZOrder(scrl, 0); 

     f.add(panel); 
     //f.pack(); 
     f.setVisible(true); 
    } 

    public static void praxStarter() throws Exception{ 
      // As a note - nrs is the holder for menu indexes 
      // info is a Map of those same menu items 
      // And one entry in info is in the form of String[]{name, class.method} 
     String[] tekst = new String[2]; 
     String kls, met; 
     Method meetod; 

     txtala.setText(""); 

     tekst = info.get(nrs.get(indez)); 
     kls = tekst[1].substring(0, tekst[1].indexOf(".")); 
     met = tekst[1].substring(tekst[1].indexOf(".") + 1, tekst[1].length()); 

     meetod = Class.forName(kls).getMethod(met); 
     meetod.setAccessible(true); 
     meetod.invoke(Class.forName(kls).newInstance()); 
    } 

而且,这里将是一个类图(或企图拉拢)存在的一个例子:

提前
public class brush extends Applet { 
public void paint(Graphics g) { 
     int x0 = 150; 
     int y0 = 150; 
     int r = 100; 
     int x, y; 
     double t; 

     int w = getWidth(); 
     int h = getHeight(); 

     x0 = w/2; 
     y0 = h/2; 

     g.setColor(Color.white); 
     g.fillRect(0, 0, w, h); 

     g.setColor(Color.black); 

     for (t = -Math.PI; t < Math.PI; t = t + Math.PI/16) { 
      x = (int) (r * Math.cos(t) + x0); 
      y = (int) (r * Math.sin(t) + y0); 
      g.drawLine(x0, y0, x, y); 
     } 
    } 
} 

感谢您的任何答案和建议!

编辑: 下面是截图: Screenshots

左边是常规,打印文本上屏模组。在右边是隐藏的文本部分,JPanel显示,JPanel从pilt = new JPanel()部分绘制,但在主类之外没有任何其他令人费解的修改。

编辑:我试图用多种方式绘制,尝试解决搜索出现,例如,

`txtala.setVisible(false); 
scrl.setVisible(false); 
pilt.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
pilt.setVisible(true); 
//bi = new BufferedImage(pilt.getWidth() + 1000, pilt.getHeight() + 1000, BufferedImage.TYPE_INT_ARGB); 
Graphics g = pilt.getGraphics(); 
g.setColor(Color.black); 
g.drawLine(0, 0, 1000, 1000); 
g.drawString("OLOLOLOLO!", 30, 50); 
pilt.paint(g); 
pilt.update(g); 
//g.dispose(); 
//repaint();` 
+0

你的意思是调用方法'meetod.invoke(...)'应该能画画吗? – denisw

+0

嗯,是的,排序的,meetod.invoke就是调用方法.. – Thara

+0

目前尚不清楚,我在所有正是你想要完成的任务。也许你可以发布你的UI的图像?也许这会使预期的行为更清晰。 – denisw

回答

0

Ohkay,所以我走了简单的路。而不是在主窗口中使用相同的窗格,我刚刚创建了一个新的弹出窗口并将框架放在该窗口中。我知道,这不是最好的解决方案,而是干草,比没有好。如果我设法找到一个没有弹出窗口的方法,那么我会在这里发布它。在此之前,祝你们好运!