C# equivalent to this Java code (simple Swing GUI)
Here is some Java code that creates a JFrame, adds a JTextField, a JButton
(with Listener) and a JPanel to the JFrame:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class MySimpleSwingGuiExample {
static JTextField myTextfield;
static JFrame myFrame;
static JButton myButton;
static JPanel myPanel;
static int buttonPressCount = 0;
public static void main(String[] args) {
//create simple frame
myFrame = new JFrame("My Simple Gui");
//create simple textfield
myTextfield = new JTextField();
myTextfield.setEditable(false);
myTextfield.setText("press button...");
//create button with listener (console output)
myButton = new JButton("my button");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
buttonPressCount++;
myTextfield.setText(String.valueOf(buttonPressCount));
}
});
//create panel with layout, add textfield and button
myPanel = new JPanel();
myPanel.setLayout(new BorderLayout());
myPanel.add(myTextfield, BorderLayout.NORTH);
myPanel.add(myButton, BorderLayout.SOUTH);
//add to frame and configure frame
myFrame.add(myPanel, BorderLayout.CENTER);
myFrame.setVisible(true);
myFrame.pack();
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
My question is:
How could a C#-translation of this code look? The translation should not
make use of fancy subclassing etc., like all those Visual Studio Express
"drag and drop" tutorials from the Internet.
On the internet there are various tutorials that create C# Guis by
dragging and dropping them. This is not the way i want to design my code
and also the reason why i thought a post would be a good starting point.
No comments:
Post a Comment