Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java application

//Imports are listed in full to show what's being used //could just import javax.swing.* and java.awt.* etc.. import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JComboBox; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JList; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class GuiApp1 { //Note: Typically the main method will be in a //separate class. As this is a simple one class //example it's all in the one class. public static void main(String[] args) { new GuiApp1(); } public GuiApp1() { JFrame guiFrame = new JFrame(); //make sure the program exits when the frame closes guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guiFrame.setTitle("Example GUI"); guiFrame.setSize(300,250); //This will center the JFrame in the middle of the screen guiFrame.setLocationRelativeTo(null); //Options for the JComboBox String[] fruitOptions = {"Apple", "Apricot", "Banana" ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"}; //Options for the JList String[] vegOptions = {"Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"}; //The first JPanel contains a JLabel and JCombobox final JPanel comboPanel = new JPanel(); JLabel comboLbl = new JLabel("Fruits:"); JComboBox fruits = new JComboBox(fruitOptions); comboPanel.add(comboLbl); comboPanel.add(fruits); //Create the second JPanel. Add a JLabel and JList and //make use the JPanel is not visible. final JPanel listPanel = new JPanel(); listPanel.setVisible(false); JLabel listLbl = new JLabel("Vegetables:"); JList vegs = new JList(vegOptions); vegs.setLayoutOrientation(JList.HORIZONTAL_WRAP); listPanel.add(listLbl); listPanel.add(vegs); JButton vegFruitBut = new JButton( "Fruit or Veg"); //The ActionListener class is used to handle the //event that happens when the user clicks the button. //As there is not a lot that needs to happen we can //define an anonymous inner class to make the code simpler. vegFruitBut.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { //When the fruit of veg button is pressed //the setVisible value of the listPanel and //comboPanel is switched from true to //value or vice versa. listPanel.setVisible(!listPanel.isVisible()); comboPanel.setVisible(!comboPanel.isVisible()); } }); //The JFrame uses the BorderLayout layout manager. //Put the two JPanels and JButton in different areas. guiFrame.add(comboPanel, BorderLayout.NORTH); guiFrame.add(listPanel, BorderLayout.CENTER); guiFrame.add(vegFruitBut,BorderLayout.SOUTH); //make sure the JFrame is visible guiFrame.setVisible(true); } }
Comment

PREVIOUS NEXT
Code Example
Java :: java returns null 
Java :: arraylist sort 
Java :: java map sorted by key 
Java :: java spring mvc 
Java :: calculating the percentile in java 
Java :: inorder traversal 
Java :: java convert int to string 
Java :: join strings in java 
Java :: how to use a while statement with char in java 
Java :: javafx combobox cell 
Java :: java evaluate two expressions in if statemenmt 
Java :: Method Overloading by changing the data type of parameters Java 
Java :: Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (execution: default-compile, phase: compile) 
Java :: factorial recursion java 
Java :: bukkit how to make a cancelable event 
Java :: Write a Java Program to check if any number is a magic number or not. 
Java :: coin flip random java 
Java :: java.lang.NullPointerException: Cannot store to double array because is null 
Java :: split by asterisk java 
Java :: java test coverage 
Java :: Java public no-arg constructors 
Java :: lombok error unnamed 
Java :: zweidimensionales array erstellen java 
Java :: public class ForLoop { public static void main(String[] args){ for(int i=1; i=n ;i++ ) System.out.println("i"); } } 
Java :: How to make sure a servlet is loaded at the application startup? 
Java :: spinner android add underline like edittext 
Java :: Java Advantages of Anonymous Classes 
Java :: java join if string is empty delimiter not used 
Java :: java how to sort custom objects in descending orde 
Java :: bukkit shutdown 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =