Java 尝试使用get scaled实例将图像添加到JButton
我收到一个错误,告诉我不能从静态上下文引用非静态方法addComponents ToPane(容器) 我刚刚开始编程两个多月,请耐心等待Java 尝试使用get scaled实例将图像添加到JButton,java,swing,constructor,jbutton,mainclass,Java,Swing,Constructor,Jbutton,Mainclass,我收到一个错误,告诉我不能从静态上下文引用非静态方法addComponents ToPane(容器) 我刚刚开始编程两个多月,请耐心等待 package prototype; import static com.oracle.util.Checksums.update; import java.awt.*; import java.util.Locale; import javax.imageio.ImageIO; import javax.swing.
package prototype;
import static com.oracle.util.Checksums.update;
import java.awt.*;
import java.util.Locale;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class NewClass {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setLocale(Locale.UK);
}
JButton button;
JButton buttonc;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
pane.setLayout(new GridBagLayout());
GridBagConstraints cc = new GridBagConstraints();
if (shouldFill) {
cc.fill = GridBagConstraints.HORIZONTAL;
}
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
try {
Image i = Toolkit.getDefaultToolkit().getImage("red.JPEG");
Image resize = i.getScaledInstance(200, 180, java.awt.Image.SCALE_SMOOTH);
ImageIcon ic = new ImageIcon(resize);
button.setIcon(ic);
buttonc = new JButton("", ic);
cc.fill = GridBagConstraints.HORIZONTAL;
cc.ipady = 20; //reset to default
cc.weightx = 0.0; //request any extra vertical space
cc.gridx = 2; //aligned with button 2
cc.gridwidth = 1; //2 columns wide
cc.gridy = 2; //third row
pane.add(button, ic);
} catch (Exception e) {
}
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
变量
ic
仅在try/catch
块的范围内可见。将变量的声明移出块
对于更新的代码,请忘记使用静态方法。代码应用于类的特定实例,因此应该是实例方法
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewClass().createAndShowGUI();
}
});
}
变量ic
仅在try/catch
块的范围内可见。将变量的声明移出块
对于更新的代码,请忘记使用静态方法。代码应用于类的特定实例,因此应该是实例方法
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewClass().createAndShowGUI();
}
});
}
变量ic
仅在try/catch
块的范围内可见。将变量的声明移出块
对于更新的代码,请忘记使用静态方法。代码应用于类的特定实例,因此应该是实例方法
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewClass().createAndShowGUI();
}
});
}
变量ic
仅在try/catch
块的范围内可见。将变量的声明移出块
对于更新的代码,请忘记使用静态方法。代码应用于类的特定实例,因此应该是实例方法
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewClass().createAndShowGUI();
}
});
}
请为我们提供一个和一个更具体的问题以帮助您请为我们提供一个和一个更具体的问题以帮助您请为我们提供一个和一个更具体的问题以帮助您请为我们提供一个和一个更具体的问题以帮助您