|
-
Jun 23rd, 2001, 01:45 PM
#1
Thread Starter
Dazed Member
Need help with code
The problem im having is pretty simple. When the changeLables()
method is invoked the text of the JButton's is not being reset...
From what i can gather, either im not passing the boolean value bl
right, or it has something to do with variable scope. Im not too sure. Any help would be cool.
/* example program created on 06/16/2001
purpose: to give an example of swings LayoutManagers
*/
// package Java.LayoutManagerApp;
import javax.swing.*;
import java.awt.*;
public class MainLayoutManagerApp{
static JFrame AppHolder;
static boolean bl = true;
static String[] LayoutManagers = {"BorderLayout","CardLayout","FlowLayout","GridLayout"};
static String[] Orientations = {"North","East","South","West","Center"};
static JButton[] LayoutTypes = new JButton[Orientations.length];
static String [] GridOrientations = {"0,0", "0,1", "1,0", "1,1", "2,0",};
static JPanel[] LayoutPanels = new JPanel[Orientations.length];
static JMenuBar menu = new JMenuBar();
static JMenuItem[] menuitem = new JMenuItem[LayoutManagers.length];
static JPanel MainPanel = new JPanel(new BorderLayout());
static Container C;
public static void main(String[] args){
MainLayoutManagerApp MLA = new MainLayoutManagerApp();
} //end main
MainLayoutManagerApp(){
AppHolder = new JFrame();
AppHolder.addWindowListener(new WindowMonitor());
C = AppHolder.getContentPane();
C.add("Center",MainPanel);
setupMenus();
C.add("North", menu);
setupPanels();
AppHolder.setSize(600,400);
AppHolder.setVisible(true);
}
static void setupMenus(){
for(int i = 0; i < LayoutManagers.length; ++i){
menuitem[i] = new JMenuItem(LayoutManagers[i]);
menuitem[i].addActionListener(new ButtonHandler());
menu.add(menuitem[i]);
}
}
static void setupLayouts(String LayoutManager){
if(LayoutManager == "BorderLayout"){
MainPanel.setLayout(new BorderLayout());
bl = true;
changeLables(bl);
switchPanels();
}
if(LayoutManager == "CardLayout"){
MainPanel.setLayout(new CardLayout());
bl = true;
changeLables(bl);
switchPanels();
}
if(LayoutManager == "FlowLayout"){
MainPanel.setLayout(new FlowLayout());
bl = true;
changeLables(bl);
switchPanels();
}
if(LayoutManager == "GridLayout"){
MainPanel.setLayout(new GridLayout(2,2));
bl = false;
changeLables(bl);
switchPanels();
}
}
static void setupPanels(){
for(int i = 0; i < Orientations.length; ++i){
LayoutPanels[i] = new JPanel();
LayoutTypes[i] = new JButton(Orientations[i]);
LayoutPanels[i].add("Center", LayoutTypes[i]);
MainPanel.add(Orientations[i], LayoutPanels[i]);
}
}
static void changeLables(boolean bl){
if(bl = true){
System.out.println(" the change lables method is invoked" + bl);
for(int i = 0; i < LayoutTypes.length; ++i){
LayoutTypes[i].setText(Orientations[i]);
}
}else{
for(int i = 0; i < LayoutTypes.length; ++i){
LayoutTypes[i].setText(GridOrientations[i]);
}
}
}
static void switchPanels(){
MainPanel.invalidate();
C.validate();
}
}
// seperate class file
// package Java.LayoutManagerApp;
import java.awt.event.*;
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ev){
String LayoutManager = ev.getActionCommand();
MainLayoutManagerApp.setupLayouts(LayoutManager);
}
}
// seperate class file
// package Java.LayoutManagerApp;
import java.awt.event.*;
public class WindowMonitor extends WindowAdapter{ // default package access
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|