0
メインJFrameの子としてJInternalFrame(または動作するもの)を開きます。 TopButtonsクラス内にイベントリスナーがあり、すべてのボタンが機能し、そこからJInternalFrameを開いています。 これは私のコードです。JFrameにJInternalFrameを追加する方法
public class MainWindow extends JFrame {
private static final long serialVersionUID = 1L;
//Constructor for the main window
public MainWindow(){
this.setLayout(new BorderLayout(5,5));
//Loads the buttons for the top of the window.
TopButtons topPanel = new TopButtons();
this.add(topPanel,BorderLayout.NORTH);
}//End of the Constructor
//Calls the constructor of the main class
public static void main(String[] args) {
MainWindow window = new MainWindow();
window.setSize(new Dimension(1000,1000));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
これは、トップボタンクラスです。
public class TopButtons extends JPanel {
private JPanel panelLeft;
private JPanel panelRight;
private static final long serialVersionUID = 1L;
/**
* Constructor creates the top panel.
* @param frame
*/
public TopButtons(){
createTopPanel();
}
//Creates the panel for the top of the window
private void createTopPanel() {
//Sets a grid layout for the top panel
setLayout(new GridLayout());
//Sets and right panels to be put inside the classes panel.
panelLeft = new JPanel();
panelRight = new JPanel();
//Sets the layouts for the left and right panels.
panelLeft.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
panelRight.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
//Adds buttons to the left panel
panelLeft.add(addButton("New Sale", "Icons/newSale.png"));
panelLeft.add(addButton("Orders", "Icons/orders.png"));
panelLeft.add(addButton("Products Search", "Icons/products.png"));
panelLeft.add(addButton("Edit Products", "Icons/editProducts.png"));
panelLeft.add(addButton("Customer Search", "Icons/customers.png"));
panelLeft.add(addButton("Stock Levels", "Icons/stock.png"));
//Adds buttons to the right panel
panelRight.add(addButton("Logout", "Icons/logout.png"));
//Adds the two panels to the grid layout
add(panelLeft);
add(panelRight);
}//End of top panel
/*Creates a button and adds an image icon.
*
*/
private JButton addButton(String name, String imgPath){
Image img = null;
Image icon = null;
JButton button = new JButton();
button.setActionCommand(name);
button.setToolTipText(name);
//Gets the image from the image class.
try {
img = Images.getImage(imgPath);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
//Scales the image to the size that is best for appearance.
icon = img.getScaledInstance(40, 40, java.awt.Image.SCALE_SMOOTH) ;
//Adds icon to button and removes the buttons margins.
button.setIcon(new ImageIcon(icon));
button.setMargin(new Insets(0,0,0,0));
//Listener for the button.
button.addActionListener(new Listener());
return button;
}
/**
* Private listener class to handle events for the panels.
* @author Carl
*
*/
private class Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//Checks the action command to see which button is pressed.
if(((JButton)e.getSource()).getActionCommand().equals("New Sale")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
if(((JButton)e.getSource()).getActionCommand().equals("Customer Search")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
if(((JButton)e.getSource()).getActionCommand().equals("Products Search")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
if(((JButton)e.getSource()).getActionCommand().equals("Orders")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
if(((JButton)e.getSource()).getActionCommand().equals("Stock Levels")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
if(((JButton)e.getSource()).getActionCommand().equals("Logout")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
if(((JButton)e.getSource()).getActionCommand().equals("Edit Products")){
//TODO Program button.
System.out.println(((JButton) e.getSource()).getActionCommand());
}
}
}//End of private class.
クラス
の} //終わりと子JInternalFrameのようにそれを追加する方法を知らないので、これは何のコードEditProductsではありません。
public class EditProducts extends JInternalFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public EditProducts(){
this.add(new JLabel("hello"));
}
}
What I want the app to look like
「JDesktopPane」が必要です(https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.htmlを参照)。 – Berger