2012-12-13 119 views
2

如何使用更改的內容更新Java框架?更新框架或重新驗證/重新繪製面板

我想更新一個框架或更新內容的面板。我該如何使用這個

這裏是我想重新驗證框架或重新繪製主板或任何工作 我已經嘗試了一些東西,但都沒有工作。

public void actionPerformed(ActionEvent e) { 
     //System.out.println(e.getActionCommand()); 
     if (e.getActionCommand().equals("advance")) { 
      multi--; 
        // Revalidate update repaint here <<<<<<<<<<<<<<<<<<< 
     } 
     else if (e.getActionCommand().equals("reverse")) { 
      multi++; 
        // Revalidate update repaint here <<<<<<<<<<<<<<<<<<<  
     } 
     else { 
      openURL(e.getActionCommand()); 
     } 

     } 

這裏是整個Java文件

/* 
* 
* 
*/ 

package build; 

import java.lang.reflect.Method; 
import javax.swing.JOptionPane; 
import java.util.Arrays; 
import java.util.*; 
import java.util.ArrayList; 

import javax.swing.*; 
import javax.swing.AbstractButton; 
import javax.swing.JScrollPane; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.ImageIcon; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

/* 
* ButtonDemo.java requires the following files: 
* images/right.gif 
* images/middle.gif 
* images/left.gif 
*/ 
public class StockTable extends JPanel 
         implements ActionListener { 
    static int multi = 1; 
    int roll = 0; 
    static TextVars textvars = new TextVars(); 
    static final String[] browsers = { "firefox", "opera", "konqueror", "epiphany", 
      "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" }; 
    JFrame frame; 
    JPanel mainpanel, panel1, panel2, panel3, panel4, panel2left, panel2center, panel2right; 
     JButton stknames_btn[] = new JButton[textvars.getNumberOfStocks()]; 
    JLabel label[] = new JLabel[textvars.getNumberOfStocks()]; 
    JLabel headlabel, dayspan, namelabel; 
    JRadioButton radioButton; 
    JButton button; 
    JScrollPane scrollpane; 
    int wid = 825; 
    public JPanel createContentPane() { 

     mainpanel = new JPanel(); 
     mainpanel.setPreferredSize(new Dimension(wid, 800)); 
     mainpanel.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     panel1 = new JPanel(); 
     panel1.setPreferredSize(new Dimension(wid, 25)); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.insets = new Insets(0,0,0,0); 
     mainpanel.add(panel1, c); 

     // Panel 2------------ 
     panel2 = new JPanel(); 
     panel2.setPreferredSize(new Dimension(wid, 51)); 
     c.gridx = 0; 
     c.gridy = 1; 
     c.insets = new Insets(0,0,0,0); 
     mainpanel.add(panel2, c); 
     panel2left = new JPanel(); 
     panel2left.setPreferredSize(new Dimension(270, 51)); 
     c.gridx = 0; 
     c.gridy = 1; 
     c.insets = new Insets(0,0,0,0); 
     panel2.add(panel2left, c); 
     panel2center = new JPanel(); 
     panel2center.setPreferredSize(new Dimension(258, 51)); 
     c.gridx = 1; 
     c.gridy = 1; 
     c.insets = new Insets(0,0,0,0); 
     panel2.add(panel2center, c); 
     panel2right = new JPanel(); 
     panel2right.setPreferredSize(new Dimension(270, 51)); 
     c.gridx = 2; 
     c.gridy = 1; 
     c.insets = new Insets(0,0,0,0); 
     panel2.add(panel2right, c); 
     // ------------------ 

     panel3 = new JPanel(); 
     panel3.setLayout(new GridBagLayout()); 
     scrollpane = new JScrollPane(panel3); 
     scrollpane.setPreferredSize(new Dimension(wid, 675)); 
     c.gridx = 0; 
     c.gridy = 2; 
     c.insets = new Insets(0,0,0,0); 
     mainpanel.add(scrollpane, c); 


      ImageIcon leftButtonIcon = createImageIcon("images/right.gif"); 

      //b1 = new JButton("Disable middle button", leftButtonIcon); 
      //b1.setVerticalTextPosition(AbstractButton.CENTER); 
      //b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales 
      //b1.setMnemonic(KeyEvent.VK_D); 
      //b1.setActionCommand("disable"); 


      //Listen for actions on buttons 1 
      //b1.addActionListener(this); 

      //b1.setToolTipText("Click this button to disable the middle button."); 

      //Add Components to this container, using the default FlowLayout. 
      //add(b1); 


     headlabel = new JLabel("hellorow1"); 
      c.gridx = 0;  
      c.gridy = 0;  
     c.insets = new Insets(0, 0, 0, 0); 
     panel1.add(headlabel, c); 

     radioButton = new JRadioButton("Percentage"); 
      c.gridx = 2;  
      c.gridy = 0;  
     c.insets = new Insets(0, 0, 0, 0); 
     panel1.add(radioButton, c); 

     radioButton = new JRadioButton("Days Range"); 
      c.gridx = 3;  
      c.gridy = 0;  
     c.insets = new Insets(0, 0, 0, 0); 
     panel1.add(radioButton, c); 

     radioButton = new JRadioButton("Open/Close"); 
      c.gridx = 4;  
      c.gridy = 0;  
     c.insets = new Insets(0, 0, 0,0); 
     panel1.add(radioButton, c); 


     button = new JButton("<<"); 
     button.setPreferredSize(new Dimension(50, 50)); 
     button.setActionCommand("reverse"); 
     button.addActionListener(this); 
      c.gridx = 0;  
      c.gridy = 1;  
     c.insets = new Insets(0, 0, 0, 0); 
      panel2left.add(button, c); 

     dayspan = new JLabel("hellorow2"); 
     dayspan.setHorizontalAlignment(JLabel.CENTER); 
     dayspan.setVerticalAlignment(JLabel.CENTER); 
     dayspan.setPreferredSize(new Dimension(270, 50)); 
      c.gridx = 1;  
      c.gridy = 1; 
     c.insets = new Insets(0, 0, 0, 0);  
      panel2center.add(dayspan, c); 

     button = new JButton(">>"); 
     button.setPreferredSize(new Dimension(50, 50)); 
     button.setActionCommand("advance"); 
     button.addActionListener(this); 
     if (multi == 0) { 
      button.setEnabled(false); 
     } 
     else { 
      button.setEnabled(true); 
     } 
      c.gridx = 2;  
      c.gridy = 1;  
     c.insets = new Insets(0, 0, 0, 0); 
      panel2right.add(button, c); 

     int availSpace_int = textvars.getStocks().size()-textvars.getNumberOfStocks()*7; 

     ArrayList<String[]> stocknames = textvars.getStockNames(); 
     ArrayList<String[]> stocks = textvars.getStocks(); 
     for (int column = 0; column < 8; column++) { 
      for (int row = 0; row < textvars.getNumberOfStocks(); row++) { 

       if (column==0) { 
        if (row==0) { 
         namelabel = new JLabel(stocknames.get(0)[0]); 
         namelabel.setVerticalAlignment(JLabel.CENTER); 
         namelabel.setHorizontalAlignment(JLabel.CENTER); 
         namelabel.setPreferredSize(new Dimension(100, 25)); 
         c.gridx = column; 
         c.gridy = row; 
         c.insets = new Insets(0, 0, 0, 0); 
         panel3.add(namelabel, c); 
        } 
        else { 
         stknames_btn[row] = new JButton(stocknames.get(row)[0], leftButtonIcon); 
         stknames_btn[row].setVerticalTextPosition(AbstractButton.CENTER); 
         stknames_btn[row].setActionCommand(stocknames.get(row)[1]); 
         stknames_btn[row].addActionListener(this); 
         stknames_btn[row].setToolTipText("go to Google Finance "+stocknames.get(row)[0]); 
         stknames_btn[row].setPreferredSize(new Dimension(100, 25)); 
         c.gridx = column; 
         c.gridy = row; 
         c.insets = new Insets(0, 0, 0, 0); 
         //scrollpane.add(stknames[row], c); 
         panel3.add(stknames_btn[row], c); 
        } 
       } 
       else { 
         label[row]= new JLabel(textvars.getStocks().get(columnMulti(multi))[1]); 
         label[row].setBorder(BorderFactory.createLineBorder(Color.black)); 
         label[row].setVerticalAlignment(JLabel.CENTER); 
         label[row].setHorizontalAlignment(JLabel.CENTER); 
         label[row].setPreferredSize(new Dimension(100, 25));  
         c.gridx = column; 
         c.gridy = row; 
         c.insets = new Insets(0,0,0,0); 
         panel3.add(label[row], c); 
       } 

      } 
     } 

     return mainpanel; 
     } 

     public void actionPerformed(ActionEvent e) { 
     //System.out.println(e.getActionCommand()); 
     if (e.getActionCommand().equals("advance")) { 
      multi--; 
     } 
     else if (e.getActionCommand().equals("reverse")) { 
      multi++;   
     } 
     else { 
      openURL(e.getActionCommand()); 
     } 

     } 

     /** Returns an ImageIcon, or null if the path was invalid. */ 
     protected static ImageIcon createImageIcon(String path) { 
      java.net.URL imgURL = StockTable.class.getResource(path); 
      if (imgURL != null) { 
        return new ImageIcon(imgURL); 
      } else { 
        System.err.println("Couldn't find file: " + path); 
        return null; 
      } 
     } 

    public static void openURL(String url) { 
     String osName = System.getProperty("os.name"); 
     try { 
      if (osName.startsWith("Mac OS")) { 
       Class<?> fileMgr = Class.forName("com.apple.eio.FileManager"); 
       Method openURL = fileMgr.getDeclaredMethod("openURL", 
        new Class[] {String.class}); 
       openURL.invoke(null, new Object[] {url}); 
         } 
      else if (osName.startsWith("Windows")) {  
       Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); 
      } 
      else { //assume Unix or Linux 
       boolean found = false; 
       for (String browser : browsers) 
        if (!found) { 
         found = Runtime.getRuntime().exec(
         new String[] {"which", browser}).waitFor() == 0; 
         if (found) 
          Runtime.getRuntime().exec(new String[] {browser, url}); 
        } 
        if (!found) 
         throw new Exception(Arrays.toString(browsers)); 
        } 
       } 
     catch (Exception e) { 
      JOptionPane.showMessageDialog(null, 
       "Error attempting to launch web browser\n" + e.toString()); 
     } 
    } 
    int reit = 0; 
    int start = textvars.getStocks().size()-((textvars.getNumberOfStocks()*5)*7)-1; 
    public int columnMulti(int multi) { 
     reit++; 
     start++; 
     if (reit == textvars.getNumberOfStocks()) { 
      reit = 0; 
      start=start+64; 
     } 
     //start = start - (multi*(textvars.getNumberOfStocks())); 
     return start; 
    } 

     /** 
     * Create the GUI and show it. For thread safety, 
     * this method should be invoked from the 
     * event-dispatching thread. 
     */ 
     private static void createAndShowGUI() { 

     //Create and set up the window. 
      JFrame frame = new JFrame("Stock Table"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      //Create and set up the content pane. 
      StockTable newContentPane = new StockTable(); 
      //newContentPane.setOpaque(true); //content panes must be opaque 
      //frame.setContentPane(newContentPane); 
     frame.setContentPane(newContentPane.createContentPane()); 
     frame.setSize(800, 800); 


      //Display the window. 
      frame.pack(); 
      frame.setVisible(true); 


     } 

    public static void main(String[] args) { 
      //Schedule a job for the event-dispatching thread: 
      //creating and showing this application's GUI. 
      javax.swing.SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         createAndShowGUI(); 
        } 
      }); 
     } 
} 
+0

你到底要更新? – MadProgrammer

+0

我想用更改後的內容更新主面板,與最初顯示的內容相同,但表格和單選按鈕的不同數字發生了變化,可更改的前後4個按鈕基本上重新運行createContentPane()並更新主面板一個新的框架 – user1516251

+0

我是否需要使用超級,我不清楚如何執行這個 – user1516251

回答

1

你發現自己想「再製造」的UI基於一個小的變化,應提高警鐘的時刻。有些情況下,這將是一個好主意,但在大多數情況下,這是一個糟糕的設計的標誌。

讓我們先從...

frame.setContentPane(newContentPane.createContentPane()); 

這是一個壞主意。您創建一個JPanel只是爲了創建另一個JPanel。突然之間,你還沒有失去UI的上下文。

取而代之的是createContentPane的,簡單的結構,你從StockTable窗格的構造UI,並添加到幀...更多類似...

frame.setContentPane(newContentPane); 

擺脫mainPanel並使用StockTable面板直。

我不能運行你的代碼,但它看起來好像你試圖「模擬」一個表格佈局。相反,簡化你的生活並學會使用JTable。更新表更容易(更不用說看起來更好),如果你做...

+1

我不知道jtable是一個選項我會使用你的建議 – user1516251

0

我的建議是,如果你想更新swing component總是使用事件調度程序線程。

我想,這可能幫助你

public void actionPerformed(ActionEvent e) { 
     //System.out.println(e.getActionCommand()); 
     if (e.getActionCommand().equals("advance")) { 
      multi--; 

        Runnable edt = new Runnable() { 
        public void run() { 
         /*Update your components here*/ 
         } 
        }; 

      SwingUtilities.invokeLater(edt); 
      } 
     else if (e.getActionCommand().equals("reverse")) { 
      multi++; 
      Runnable edt= new Runnable() { 
        public void run() { 
         /*Update your components here*/ 
        } 
       }; 

       SwingUtilities.invokeLater(edt); 
     } 
     else { 
      openURL(e.getActionCommand()); 
     } 

     } 
    } 
+0

我在這裏使用wihtin/*更新組件* /使這個if語句if(multi == 0){button.setEnabled(false); }工作 其他{ button.setEnabled(true); } – user1516251

+0

如果我使用mainpanel.removeAll()將使主面板重新加載第一個方法中的信息 – user1516251