[英]Java : using graphics component within an action listener
我已經用Diferent JButtons制作了一個JFrame,我想從另一個類中獲取圖像。 有任何想法嗎? 或如何借鑒同一個班級但要采取的行動? 因為它不允許我做任何繪圖...我的編譯器總是給我錯誤消息
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class red extends JFrame {
public JButton b;
public JButton b1;
public JButton b2;
public JButton b3;
public JButton b4;
public static Image p;
public static Graphics g;
public red() throws IOException {
gui1 x = new gui1();
setTitle(" ");
setSize(1200,700);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b= new JButton("click");
b1= new JButton();
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e0){
b1.setBounds(0, 0, 200, 200);
b.show(false);
add(x);
}
});
b.setBounds(0, 0, 100, 100);
add(b1);
add(b);
setVisible(true);
}
public static void main(String[] args) throws IOException {
red k = new red();
}
}
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class gui1 extends Canvas {
public static Image p;
public void paint(Graphics g){
g.drawImage(p, 700, 200, 100, 100, this);
}
{
try {
p= ImageIO.read(new File("Lighthouse.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
! 我在您的代碼中看到很多錯誤(即使在糾正編譯錯誤之后):
您沒有遵循Java命名約定 :
類名應為名詞,大小寫混合,每個內部單詞的首字母大寫
red
是一個名詞,但它應該更具描述性且大寫。 gui1
您正在擴展JFrame
,用簡單的英語說: red
是一個 JFrame
,您應該真正避免這樣做,而應基於JPanel
創建您的GUI ...請參見Java Swing在類內部使用extended JFrame vs callint
您正在設置大小(對於正在使用的JButton
大小,這是一個非常大的窗口),而不是使用pack()
您正在使用null-layout
,而像素完美的GUI似乎是為Swing新手創建復雜GUI的最簡單方法,您使用它們的次數越多,將來會發現與此相關的更多問題,那么很難解決這些問題維護並導致隨機問題,它們不會調整大小,等等。請閱讀空布局是邪惡的 , 為什么在Swing中使用空布局會讓人皺眉呢? 有關為什么應避免使用它以及為什么應更改GUI以使其與Layout Managers和Empty Borders一起使用以在組件之間留出更多間距的更多信息。
您正在使用不推薦使用的方法JFrame#show()
,而應使用JFrame#setVisible(...)
。
與第4點相關,您不應調用setBounds(...)
方法,而應將計算結果發送給布局管理器。
您沒有將程序放在事件調度線程(EDT)上 ,Swing也不是線程安全的,可以通過如下更改main()
方法來解決此問題:
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { //Your constructor here } }); }
您在混合AWT和Swing組件,而不是使用AWT的Canvas
,而是使用具有更多功能和支持的Swing的JPanel
。
一旦將圖像打包到JAR文件中,它們將成為嵌入式資源,因此明智的做法是開始將其視為已存在,而不是像Embedded-resource標簽中所示的那樣作為外部文件。
從Canvas
更改為JPanel
您應該覆蓋其paintComponent(...)
方法而不是paint(...)
並將其稱為super.paintComponent(g)
方法作為第一行,也不要忘記添加@Overrides
注釋。 請參閱有關Swing自定義繪畫的教程 。
您正在濫用static
關鍵字,請參閱static
關鍵字如何工作?
看到上述所有錯誤后,我建議您返回並學習該語言的基礎知識 ,然后再開始使用圖形化環境,這只會給您的學習帶來更多困難。
據我了解,您想在按鈕單擊上繪制圖像,如果是這種情況,則可以將圖像包裝在JLabel
,然后將該JLabel
添加到JPanel
,然后將其添加到父JPanel
,然后將其添加到JFrame
:
如您在上面的GIF中所看到的,用戶按下按鈕后,該圖標就會顯示出來。
顯然,這可以通過使用布局管理器和空白邊框的組合來使GUI更具“吸引力”,從而得到改善。
這是通過以下代碼完成的:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ImageDrawingFromOneClassToAnother {
private JFrame frame;
private JPanel pane;
private JPanel leftPane;
private JPanel rightPane;
private ImageIcon icon;
private JButton button;
private JLabel label;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ImageDrawingFromOneClassToAnother().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
icon = new ImageIcon(this.getClass().getResource("king.png")); //Read images as if they were already embedded resources
button = new JButton("Draw image");
label = new JLabel(""); //Create an empty label
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setIcon(icon); //On button click, we set the icon for the empty label
}
});
pane = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200); //Set a size for the main panel
}
};
pane.setLayout(new GridLayout(1, 2)); //The main panel
leftPane = new JPanel(); //The button panel
leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.PAGE_AXIS));
leftPane.add(button);
rightPane = new JPanel(); //The panel where the image will be drawn
rightPane.add(label);
//We add both (button and image) panels to the main panel
pane.add(leftPane);
pane.add(rightPane);
frame.add(pane); //Add the main panel to the frame
frame.pack(); //Calculate its preferred size
frame.setVisible(true); //Set it to be visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.