(公司面试题探讨) 最简单的实现看你对设计模式的理解. 实现一个窗体由两个面板组成, 当左面板上的按钮被按下, 右面板标签上出现一字符串.要求在30分钟实现多个解决方案
我是这么做的, 求其他的解决方案import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Frame extends JFrame
{
private Lpanel l = null;
private Rpanel n = null; public Frame()
{
super(); l = new Lpanel(this);
n = new Rpanel(this);
this.getContentPane().add(l, BorderLayout.WEST);
this.getContentPane().add(n, BorderLayout.CENTER);
} public void DoSth(String s)
{
n.SetLabel(s);
} public static void main(String[] args)
{
Frame fa = new Frame(); fa.setSize(300, 200);
fa.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.*;class Lpanel extends JPanel
{
private Frame lpf = null; private JButton b; public Lpanel(Frame f)
{
super();
lpf = f; b = new JButton("AAA");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
lpf.DoSth(Lpanel.this.b.getText());
// 我希望当JButton被按下时, 通过 Frame 类中的方法将JButton上的标题传送给Rpanel 中 Label
}
}); this.add(b);
}
}import javax.swing.*;
import java.awt.event.*;class Rpanel extends JPanel
{
private Frame npf = null; private JLabel label = null; public void SetLabel(String s)
{
label.setText(s);
} public Rpanel(Frame f)
{
super();
this.npf = f; this.label = new JLabel("123456");
this.add(label);
}
}
我是这么做的, 求其他的解决方案import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Frame extends JFrame
{
private Lpanel l = null;
private Rpanel n = null; public Frame()
{
super(); l = new Lpanel(this);
n = new Rpanel(this);
this.getContentPane().add(l, BorderLayout.WEST);
this.getContentPane().add(n, BorderLayout.CENTER);
} public void DoSth(String s)
{
n.SetLabel(s);
} public static void main(String[] args)
{
Frame fa = new Frame(); fa.setSize(300, 200);
fa.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.*;class Lpanel extends JPanel
{
private Frame lpf = null; private JButton b; public Lpanel(Frame f)
{
super();
lpf = f; b = new JButton("AAA");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
lpf.DoSth(Lpanel.this.b.getText());
// 我希望当JButton被按下时, 通过 Frame 类中的方法将JButton上的标题传送给Rpanel 中 Label
}
}); this.add(b);
}
}import javax.swing.*;
import java.awt.event.*;class Rpanel extends JPanel
{
private Frame npf = null; private JLabel label = null; public void SetLabel(String s)
{
label.setText(s);
} public Rpanel(Frame f)
{
super();
this.npf = f; this.label = new JLabel("123456");
this.add(label);
}
}
import java.awt.*;
import java.awt.event.*;public class LeftRight extends JFrame implements ActionListener{
private LeftPanel left;
private RightPanel right;
public LeftRight(){
left = new LeftPanel(this);
right = new RightPanel();
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(left,BorderLayout.WEST);
c.add(right,BorderLayout.EAST);
this.setSize(new Dimension(400,300));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "leftButton"){
right.change();
}
}
public static void main(String args[]){
new LeftRight().setVisible(true);
}
}class LeftPanel extends JPanel{
private LeftRight father;
private JButton button;
public LeftPanel(LeftRight f){
father = f;
button = new JButton("doIt");
button.setActionCommand("leftButton");
button.addActionListener(father);
this.setLayout(new FlowLayout());
this.add(button);
}
}class RightPanel extends JPanel{
private JLabel label;
public RightPanel(){
label = new JLabel("toChange");
this.setLayout(new FlowLayout());
this.add(label);
}
public void change(){
label.setText("changed!");
}
}