簡體   English   中英

從另一個類java向JFrame添加圖片

[英]Adding picture to JFrame from another class java

我正在嘗試制作一個程序,該程序讀取用戶選擇的文件,並且在讀取文件后-將后綴“ txt”更改為“ gif”,並將文件另存為圖片(與該文件位於同一目錄中)文件)。 問題是,此圖片變量在“ actionPerformed-method”中獲取其值,此后,我想將其添加到另一個類的框架中,但它不會顯示。 這是我的OptionsPane類中的代碼:

public class OptionsPane extends JComponent implements ActionListener{

    private JButton buttonOne = new JButton("Alt.1");
    private JButton buttonTwo = new JButton("Alt.2");
    private JButton buttonThree = new JButton("Alt.3");
    private int option;
    private JButton buttonChoose = new JButton("Choose file"); 
    private FileHandler filehandler; 
    private String picture;
    private JLabel picLabel; 

    public OptionsPane(){

        JLabel label = new JLabel("Choose optimization method", SwingConstants.CENTER);
        JPanel subPanel = new JPanel(); 
        label.setForeground(Color.CYAN); 
        label.setFont(new Font("Tahoma", Font.BOLD, 15));   
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(label);
        buttonOne.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonChoose.addActionListener(this);

        subPanel.setBackground(Color.DARK_GRAY);
        subPanel.add(buttonOne);
        subPanel.add(buttonTwo);
        subPanel.add(buttonThree);
        subPanel.add(buttonChoose);
        this.add(subPanel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == buttonOne){
            option = 1;
            System.out.println("You clicked button 1!");
        }else if(e.getSource() == buttonTwo){
            option = 2;
            System.out.println("You clicked button 2!");
        }else if(e.getSource() == buttonThree){
            option = 3;
            System.out.println("You clicked button 3!");
        }else if(e.getSource() == buttonChoose){
            System.out.println("hello");
            option = 4; 
            filehandler = new FileHandler(); 
            filehandler.read();
            picture = filehandler.getFilePath().replaceFirst("txt", "gif"); 
            picLabel = new JLabel(new ImageIcon(picture));
            this.add(picLabel); 
        }
    }
}

該框架位於“ MainFrame”類中,目前如下所示:

public class MainFrame extends JFrame{

    private JFrame frame = new JFrame(); 
    private String picture; 
    private JLabel picLabel;
    public MainFrame(){

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(1300, 800)); 
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

        OptionsPane optionspane = new OptionsPane(); 
        frame.add(optionspane);

        frame.pack(); 
        frame.setVisible(true);
        frame.setResizable(true);
    }
}

為什么在大型機中看不到圖片?

編輯現在可以使用!

https://stackoverflow.com/a/22380387/3271504

感謝您的幫助@arooaroo。 我試圖記下您寫的內容,但是當我想根據用戶選擇的文件添加圖像時仍然無法正常工作(例如,如果用戶選擇文件text1.txt,我想要相應的圖片“ text1.gif”顯示)。 在您的幫助下,當我用“ /”-斜杠鍵入特定路徑時該圖片出現了,但是當我選擇一個文件並嘗試從文件路徑中加載圖片時,該圖片沒有顯示,這是因為它帶有反斜杠在通路中。 這應該是這樣(這樣一個令人煩惱的問題):

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == buttonOne){
        option = 1;
        System.out.println("You clicked button 1!");
    }else if(e.getSource() == buttonTwo){
        option = 2;
        System.out.println("You clicked button 2!");
    }else if(e.getSource() == buttonThree){
        option = 3;
        System.out.println("You clicked button 3!");
    }else if(e.getSource() == buttonChoose){
    filehandler = new FileHandler(); 
    filehandler.read();
    filepath = filehandler.getFilePath(); 
    picture = filepath.replaceFirst("txt", "gif");
    picture = picture.replaceAll("\\\\", "/");

    ImageIcon icon = new ImageIcon(picture);
    mainFrame.setPicture(icon);

}

謝謝您的幫助!

一旦將您的GUI代碼分成不同的類(這是一件好事),您將發現GUI編程的永恆挑戰是允許它們之間存在相互依賴關系時進行干凈的通信。

在這種情況下,也許最簡單的方法是將MainFrame的引用傳遞到OptionsPane

假設您在MainFrame創建了用於設置圖片的其他方法:

public class MainFrame extends JFrame{

  // all instance vars as before

  public MainFrame() {
     // same as before except for this line...
     OptionsPane optionspane = new OptionsPane(this); 
  }

  public void setPicture(JLabel pictureLabel) {
     // add code here for adding the picture...
     // That's an exercise for yourself, or another question ;)
  }

}

然后在您的OptionsPane類中:

....
private MainFrame mainFrame;  // add a new instance var

public OptionsPane(MainFrame mainFrame) {
  this.mainFrame = mainFrame;
  // ... rest of the code same as before
}

@Override
public void actionPerformed(ActionEvent e) {
    //...
        picture = filehandler.getFilePath().replaceFirst("txt", "gif"); 
        picLabel = new JLabel(new ImageIcon(picture));
        mainFrame.setPicture(picLabel); // <-- This is where you communicate with the mainFrame instance 
    //...
}

編輯

盡管我的原始答案提供了有效且正確的解決方案,但很明顯,OP需要一個完全有效的示例,包括加載顯示結果圖像的代碼。 這是一個示例程序。

public class OptionsPane extends JComponent implements ActionListener {

    private JButton buttonOne = new JButton("Alt.1");
    private JButton buttonTwo = new JButton("Alt.2");
    private JButton buttonThree = new JButton("Alt.3");
    private int option;
    private JButton buttonChoose = new JButton("Choose file");
    private String picture;
    private JLabel picLabel;
    private MainFrame mainFrame;

    public OptionsPane(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        JLabel label = new JLabel("Choose optimization method", SwingConstants.CENTER);
        JPanel subPanel = new JPanel();
        label.setForeground(Color.CYAN);
        label.setFont(new Font("Tahoma", Font.BOLD, 15));
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(label);
        buttonOne.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonChoose.addActionListener(this);

        subPanel.setBackground(Color.DARK_GRAY);
        subPanel.add(buttonOne);
        subPanel.add(buttonTwo);
        subPanel.add(buttonThree);
        subPanel.add(buttonChoose);
        this.add(subPanel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // For sake of simplicity I'm ignoring the original button logic here
        // and focussing on just getting an icon loaded in the parent frame...

        ImageIcon icon = new ImageIcon("/path/to/test/image.png");
        // Just pass the icon itself rather than a new label.
        mainFrame.setPicture(icon);

    }
}


public class MainFrame {
    // No need to extend JFrame if you're using a JFrame instance variable
    private JFrame frame = new JFrame();
    private JLabel picLabel;

    private JPanel mainPanel;
    public MainFrame() {
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBackground(Color.DARK_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(1300, 800));

        OptionsPane optionspane = new OptionsPane(this);
        mainPanel.add(optionspane, BorderLayout.NORTH);
        picLabel = new JLabel();
        picLabel.setHorizontalAlignment(JLabel.CENTER);
        mainPanel.add(picLabel, BorderLayout.CENTER);

        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(true);
    }

    public void setPicture(ImageIcon icon) {
        picLabel.setIcon(icon);
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}

請注意,我做了幾件不同的事情。 我個人總是創建一個JPanel並將其設置為主層,然后直接將其添加到框架中,而不是與rootPane搞混。 在此示例中,我使用了BorderLayout ,因為它要簡單得多。

另一件事是在初始設置中添加JLabel ,以將圖片保存到GUI。 然后您將看到我僅在setPicture()方法中更改其圖標,而不是在每個實例上添加新的JLabel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM