为什么 JToolbarbuttons 在 Netbeans IDE 上工作,但在可执行的 jar 格式上不起作用
Why do JToolbarbuttons work on Netbeans IDE but not in executable jar format
我的应用程序的 JToolbar 按钮在以 .jar 格式单击时根本没有响应,但在 Netbeans 上响应完美 IDE,我真的不明白为什么我遇到这个问题,这里是说明应用程序未响应的 JToolBarButton 的图像:
另外,我认为有问题的部分代码如下所示:
public class MainMenu extends JFrame implements ActionListener{
MyDesktopPane dp = new MyDesktopPane();
String TITLE = "The complete payroll system ";
JMenuBar menubar = new JMenuBar();
JPanel panel_Bottom = new JPanel();
JPanel panel_Top = new JPanel();
JButton btnOut = new JButton(new ImageIcon(getClass ().getResource("logout.png")));
Image img = new ImageIcon(getClass().getResource("/16.jpg")).getImage();
public MainMenu() {
super("PayRoll System");
panel_Top.setLayout(new BorderLayout());
panel_Top.setPreferredSize(new Dimension(10,150));
JToolBar tool=createJToolBar();tool.setBackground(new Color(36,121,183));
panel_Top.add(tool,BorderLayout.PAGE_START);
getContentPane().add(panel_Top,BorderLayout.PAGE_START);
getContentPane().add(dp,BorderLayout.CENTER);
btnOut.setBounds(600,230,75,35);
dp.add(btnOut);
{
btnOut.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
UnloadWindow();
}
});
}
}
public void actionPerformed(ActionEvent e){
Login log= new Login();
Object source = e.getSource();
if(source == btnOut){
log.setLogin();
dispose();
}
}
protected JToolBar createJToolBar()
{
JToolBar toolbar = new JToolBar("Toolbar");
toolbar.add(CreateJToolbarButton("Employee-Entry", "employee.png", "Emp_Entry",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Employee Position Settings", "setting.png","Settings",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("DTR", "admin.png","DTR",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("User - Settings", "inf.png","User_Settings",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Cash_Advance", "cash.png","Cash_Advance",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Employee - Payoll", "rep.png","Reports_Payroll",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Notepad", "SimpleTextEdit.png", "Notepad",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Calculator", "Calculator2.png", "Calculator",
JToolBarActionListener));
toolbar.addSeparator();
return toolbar;
}
ActionListener JToolBarActionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
empSettings set= new empSettings();
empEntry inf= new empEntry();
UserSettings panel1 = new UserSettings();
notepad note= new notepad();
BasicCalc calc= new BasicCalc();
empDeduction frameDeduct= new empDeduction();
empCashAdvance cash= new empCashAdvance();
empPayroll pay = new empPayroll();
String source = e.getActionCommand();
if(source == "Emp_Entry")
{
inf.setEntry();
}
if (source == "Settings")
{
set.setSettings();
}
if (source == "Reports_Payroll")
{
pay.setPay();
}
if (source == "DTR")
{
frameDeduct.setDeduct();
}
if (source == "Notepad")
{
note.setNote();
}
if (source == "Calculator")
{
calc.setCalc();
}
if (source == "User_Settings")
{
panel1.frameUser();
}
if(source == "Cash_Advance")
{
cash.setCash();
}
}
};
protected void paintComponent(Graphics g) {
Graphics2D graphics = (Graphics2D) g.create();
Image img = new ImageIcon(getClass().getResource("/16.jpg")).getImage();
graphics.drawImage(img, 0, 0, getWidth(), getHeight(), null);
graphics.dispose();
}
protected void UnloadWindow()
{
try
{
int reply = JOptionPane.showConfirmDialog(this,"Are you sure to exit?",TITLE,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if (reply == JOptionPane.YES_OPTION)
{
setVisible(false);
System.exit(0);
}
else {
}
}
catch(Exception e)
{}
}
public JButton CreateJToolbarButton(String srcToolTipText,String srcImageLocation,String srcActionCommand, ActionListener JToolBarActionListener)
{
JButton bttnToolbar = new JButton(new ImageIcon(getClass ().getResource (srcImageLocation)));
bttnToolbar.setActionCommand(srcActionCommand);
bttnToolbar.setToolTipText(srcToolTipText);
bttnToolbar.setCursor(new Cursor(Cursor.HAND_CURSOR));
bttnToolbar.setFont(new Font("Dialog", Font.PLAIN, 12));
bttnToolbar.addActionListener(JToolBarActionListener);
return bttnToolbar;
}
public void setMain(){
MainMenu p1= new MainMenu();
p1.setSize(1228,700);
p1.setLocation(70,15);
p1.setVisible(true);
p1.setResizable(false);
}
public static void mainPane(){
new MainMenu ();
}
如果有人能帮我解决这个问题,我将不胜感激。
我通过打开 regedit.exe 解决了这个问题,然后导航到 HKEY_CLASSES_ROOT\jarfile\shell\open\command 并更改值中的 Javaw.exe 路径:"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" % 到:"C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javaw.exe" -jar "%1" %.
"C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javaw.exe" 是我的 IDE 用来执行应用程序的 Javaw.exe 路径,没有任何问题,JToolbar 按钮现在当应用程序在 Netbeans IDE 之外启动时,工作完美。
我的应用程序的 JToolbar 按钮在以 .jar 格式单击时根本没有响应,但在 Netbeans 上响应完美 IDE,我真的不明白为什么我遇到这个问题,这里是说明应用程序未响应的 JToolBarButton 的图像:
另外,我认为有问题的部分代码如下所示:
public class MainMenu extends JFrame implements ActionListener{
MyDesktopPane dp = new MyDesktopPane();
String TITLE = "The complete payroll system ";
JMenuBar menubar = new JMenuBar();
JPanel panel_Bottom = new JPanel();
JPanel panel_Top = new JPanel();
JButton btnOut = new JButton(new ImageIcon(getClass ().getResource("logout.png")));
Image img = new ImageIcon(getClass().getResource("/16.jpg")).getImage();
public MainMenu() {
super("PayRoll System");
panel_Top.setLayout(new BorderLayout());
panel_Top.setPreferredSize(new Dimension(10,150));
JToolBar tool=createJToolBar();tool.setBackground(new Color(36,121,183));
panel_Top.add(tool,BorderLayout.PAGE_START);
getContentPane().add(panel_Top,BorderLayout.PAGE_START);
getContentPane().add(dp,BorderLayout.CENTER);
btnOut.setBounds(600,230,75,35);
dp.add(btnOut);
{
btnOut.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
UnloadWindow();
}
});
}
}
public void actionPerformed(ActionEvent e){
Login log= new Login();
Object source = e.getSource();
if(source == btnOut){
log.setLogin();
dispose();
}
}
protected JToolBar createJToolBar()
{
JToolBar toolbar = new JToolBar("Toolbar");
toolbar.add(CreateJToolbarButton("Employee-Entry", "employee.png", "Emp_Entry",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Employee Position Settings", "setting.png","Settings",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("DTR", "admin.png","DTR",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("User - Settings", "inf.png","User_Settings",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Cash_Advance", "cash.png","Cash_Advance",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Employee - Payoll", "rep.png","Reports_Payroll",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Notepad", "SimpleTextEdit.png", "Notepad",
JToolBarActionListener));
toolbar.addSeparator();
toolbar.add(CreateJToolbarButton("Calculator", "Calculator2.png", "Calculator",
JToolBarActionListener));
toolbar.addSeparator();
return toolbar;
}
ActionListener JToolBarActionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
empSettings set= new empSettings();
empEntry inf= new empEntry();
UserSettings panel1 = new UserSettings();
notepad note= new notepad();
BasicCalc calc= new BasicCalc();
empDeduction frameDeduct= new empDeduction();
empCashAdvance cash= new empCashAdvance();
empPayroll pay = new empPayroll();
String source = e.getActionCommand();
if(source == "Emp_Entry")
{
inf.setEntry();
}
if (source == "Settings")
{
set.setSettings();
}
if (source == "Reports_Payroll")
{
pay.setPay();
}
if (source == "DTR")
{
frameDeduct.setDeduct();
}
if (source == "Notepad")
{
note.setNote();
}
if (source == "Calculator")
{
calc.setCalc();
}
if (source == "User_Settings")
{
panel1.frameUser();
}
if(source == "Cash_Advance")
{
cash.setCash();
}
}
};
protected void paintComponent(Graphics g) {
Graphics2D graphics = (Graphics2D) g.create();
Image img = new ImageIcon(getClass().getResource("/16.jpg")).getImage();
graphics.drawImage(img, 0, 0, getWidth(), getHeight(), null);
graphics.dispose();
}
protected void UnloadWindow()
{
try
{
int reply = JOptionPane.showConfirmDialog(this,"Are you sure to exit?",TITLE,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if (reply == JOptionPane.YES_OPTION)
{
setVisible(false);
System.exit(0);
}
else {
}
}
catch(Exception e)
{}
}
public JButton CreateJToolbarButton(String srcToolTipText,String srcImageLocation,String srcActionCommand, ActionListener JToolBarActionListener)
{
JButton bttnToolbar = new JButton(new ImageIcon(getClass ().getResource (srcImageLocation)));
bttnToolbar.setActionCommand(srcActionCommand);
bttnToolbar.setToolTipText(srcToolTipText);
bttnToolbar.setCursor(new Cursor(Cursor.HAND_CURSOR));
bttnToolbar.setFont(new Font("Dialog", Font.PLAIN, 12));
bttnToolbar.addActionListener(JToolBarActionListener);
return bttnToolbar;
}
public void setMain(){
MainMenu p1= new MainMenu();
p1.setSize(1228,700);
p1.setLocation(70,15);
p1.setVisible(true);
p1.setResizable(false);
}
public static void mainPane(){
new MainMenu ();
}
如果有人能帮我解决这个问题,我将不胜感激。
我通过打开 regedit.exe 解决了这个问题,然后导航到 HKEY_CLASSES_ROOT\jarfile\shell\open\command 并更改值中的 Javaw.exe 路径:"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" % 到:"C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javaw.exe" -jar "%1" %.
"C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javaw.exe" 是我的 IDE 用来执行应用程序的 Javaw.exe 路径,没有任何问题,JToolbar 按钮现在当应用程序在 Netbeans IDE 之外启动时,工作完美。