eftersom mycket är generetat av WindowBuilder
Kod: Markera allt
public class TNet
{
//********************************************
//* T-nätet med getters och setters
//********************************************
private double impedance;
private double decibel;
private double VO;
private double VT;
private double A;
private double B;
private double C;
public TNet()
{
impedance = 0;
decibel = 0;
VO = 0;
VT = 0;
A = 0;
B = 0;
C = 0;
}
public void setTNet_Decibel(double dB)
{
decibel = dB;
}
public void setTNet_Impedance(double imp)
{
impedance = imp;
}
public void setTNet_VO(double vo)
{
VO = vo;
}
public void setTNet_VT(double vt)
{
VT = vt;
}
public void setTNet_A(double a)
{
A = a;
}
public void setTNet_B(double b)
{
B = b;
}
public void setTNet_C(double c)
{
C = c;
}
public double getTNet_Decibel()
{
return decibel;
}
public double getTNet_Impedance()
{
return impedance;
}
public double getTNet_VO()
{
return VO;
}
public double getTNet_VT()
{
return VT;
}
public double getTNet_A()
{
return A;
}
public double getTNet_B()
{
return B;
}
public double getTNet_C()
{
return C;
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setVisible(true);
shell.setBackground(SWTResourceManager.getColor(255, 248, 220));
shell.setSize(620, 720);
shell.setText("SWT Application - T Net Matched Attenuator");
shell.setLayout(null);
shell.setLocation(700, 0);
//*****************************************************************************
//* Skala, potentiometer. Öka eller minska VT.
//*****************************************************************************
Scale VTScale = new Scale(shell, SWT.HORIZONTAL);
VTScale.setEnabled(false);
//*****************************************************************************
//* Skylt som presenterar inställd VT m.h.a skjutpotentiometern
//*****************************************************************************
CLabel VTvalueLabel = MyFactory.createCLabel(shell, SWT.NONE);
//*****************************************************************************
//* Skylt som informerar om beräknad VO efter tryck på kalkylknapp
//*****************************************************************************
CLabel lblYLabel = TFactory.createCLabel(shell, SWT.NONE);
//*****************************************************************************
//* Skyltar och Displayer som presenterar beräknade resistansvärden
//*****************************************************************************
CLabel lblA = MyFactory.createCLabel(shell, 0);
CLabel lblB = MyFactory.createCLabel(shell, 0);
CLabel lblC = MyFactory.createCLabel(shell, 0);
//*****************************************************************************
//* Knapp för att beräkna VO. Hämtar data från valda Radioknappar
//*****************************************************************************
Button btnCalc = new Button(shell, SWT.NONE);
btnCalc.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnCalc.setText("Calculate VO");
btnCalc.setBackground(SWTResourceManager.getColor(255, 204, 51));
btnCalc.setBounds(467, 433, 127, 59);
btnCalc.setEnabled(true);
//*****************************************************************************
//* Vid nya indataval, dB och impedans, så återställs skyltar till utgångsläge
//*****************************************************************************
class resetLBL
{
public resetLBL(){}
public void doReset()
{
VTScale.setEnabled(false);
lblYLabel.setText("VO");
VTvalueLabel.setText("VT");
lblA.setText("A");
lblB.setText("B");
lblC.setText("C");
VTScale.setSelection(50);
btnCalc.setEnabled(true);
}
}
resetLBL resetLabels = new resetLBL();
//****************************************************************************
//* Schema för nätet
//****************************************************************************
CLabel lblNewLabel = new CLabel(shell, SWT.NONE);
lblNewLabel.setBounds(10, 10, 584, 286);
lblNewLabel.setTopMargin(0);
lblNewLabel.setBackground(SWTResourceManager.getImage(TMainWindow.class, "/org/eclipse/wb/swt/SCHEMA.png"));
//****************************************************************************
//* Infoskylt, val av impedans
//****************************************************************************
CLabel lblVljDmpning = new CLabel(shell, SWT.NONE);
lblVljDmpning.setEnabled(false);
lblVljDmpning.setBounds(10, 302, 212, 59);
lblVljDmpning.setBottomMargin(0);
lblVljDmpning.setTopMargin(8);
lblVljDmpning.setForeground(SWTResourceManager.getColor(255, 255, 255));
lblVljDmpning.setBackground(SWTResourceManager.getColor(153, 0, 0));
lblVljDmpning.setLeftMargin(15);
lblVljDmpning.setFont(SWTResourceManager.getFont("Sitka Display", 18, SWT.BOLD));
lblVljDmpning.setText("V\u00E4lj Impedans");
//*****************************************************************************
//* Gruppera Radioknappar impedans
//*****************************************************************************
formToolkit.setBackground(SWTResourceManager.getColor(255, 245, 238));
Group group = new Group(shell, SWT.NONE);
group.setBounds(233, 302, 361, 59);
formToolkit.adapt(group);
formToolkit.paintBordersFor(group);
group.setLayout(null);
//***************************************************************
// 3 st. Radioknappar för val av impedans Tillhör group
//***************************************************************
Button btnOhm = new Button(group, SWT.RADIO); // 50 ohm
Button btnOhm_1 = new Button(group, SWT.RADIO); // 75 ohm
Button btnOhm_2 = new Button(group, SWT.RADIO); // 600 ohm
btnOhm.setSelection(true); // 50 ohm default
//* 50 ohm lyssnare
btnOhm.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnOhm.setSelection(true);
btnOhm_1.setSelection(false);
btnOhm_2.setSelection(false);
resetLabels.doReset();
}
});
btnOhm.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnOhm.setAlignment(SWT.CENTER);
btnOhm.setBounds(10, 18, 90, 30);
btnOhm.setText("50 ohm");
formToolkit.adapt(btnOhm, true, true);
//* 75 ohm lyssnare
btnOhm_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnOhm.setSelection(false);
btnOhm_1.setSelection(true);
btnOhm_2.setSelection(false);
resetLabels.doReset();
}
});
btnOhm_1.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnOhm_1.setAlignment(SWT.CENTER);
btnOhm_1.setBounds(106, 19, 90, 30);
btnOhm_1.setText("75 ohm");
formToolkit.adapt(btnOhm_1, true, true);
//* 600 ohm lyssnare
btnOhm_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnOhm.setSelection(false);
btnOhm_1.setSelection(false);
btnOhm_2.setSelection(true);
resetLabels.doReset();
}
});
btnOhm_2.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnOhm_2.setAlignment(SWT.CENTER);
btnOhm_2.setBounds(202, 19, 90, 30);
btnOhm_2.setText("600 ohm");
formToolkit.adapt(btnOhm_2, true, true);
//*******************************************************************
// info.skylt med R på. Tillhör group. Samma färg som gruppen, group
//*******************************************************************
CLabel lblR = new CLabel(group, SWT.NONE);
lblR.setEnabled(false);
lblR.setForeground(SWTResourceManager.getColor(0, 128, 128));
lblR.setBounds(298, 10, 53, 47);
lblR.setFont(SWTResourceManager.getFont("Times New Roman", 20, SWT.BOLD));
lblR.setAlignment(SWT.CENTER);
formToolkit.adapt(lblR);
formToolkit.paintBordersFor(lblR);
lblR.setText("R"); // R
group.setTabList(new Control[]{btnOhm, btnOhm_1, btnOhm_2});
//******************************************************************
// info.skylt Välj Dämpning
//******************************************************************
CLabel lblVljDmpning_1 = new CLabel(shell, SWT.NONE);
lblVljDmpning_1.setEnabled(false);
lblVljDmpning_1.setTopMargin(8);
lblVljDmpning_1.setText("V\u00E4lj D\u00E4mpning");
lblVljDmpning_1.setLeftMargin(15);
lblVljDmpning_1.setForeground(SWTResourceManager.getColor(255, 255, 255));
lblVljDmpning_1.setFont(SWTResourceManager.getFont("Sitka Display", 18, SWT.BOLD));
lblVljDmpning_1.setBottomMargin(0);
lblVljDmpning_1.setBackground(SWTResourceManager.getColor(153, 0, 0));
lblVljDmpning_1.setBounds(10, 369, 212, 59);
//*****************************************************************************
//* Gruppera Radioknappar dämpning
//*****************************************************************************
Group group_1 = new Group(shell, SWT.NONE);
group_1.setLayout(null);
group_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
group_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
group_1.setBounds(233, 368, 361, 59);
formToolkit.adapt(group_1);
formToolkit.paintBordersFor(group_1);
//*******************************************************************
// 5 st. Radioknappar för val av decibel.Tillhör group_1
//*******************************************************************
Button btnDb = new Button(group_1, SWT.RADIO); // 1dB
Button btnDb_1 = new Button(group_1, SWT.RADIO); // 2dB
Button btnDb_2 = new Button(group_1, SWT.RADIO); // 3dB
Button btnDb_3 = new Button(group_1, SWT.RADIO); // 6dB
Button btnDb_4 = new Button(group_1, SWT.RADIO); // 12dB
btnDb_2.setSelection(true); // 3dB default
btnDb.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnDb.setSelection(true);
btnDb_1.setSelection(false);
btnDb_2.setSelection(false);
btnDb_3.setSelection(false);
btnDb_4.setSelection(false);
resetLabels.doReset();
}
});
btnDb_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnDb.setSelection(false);
btnDb_1.setSelection(true);
btnDb_2.setSelection(false);
btnDb_3.setSelection(false);
btnDb_4.setSelection(false);
resetLabels.doReset();
}
});
btnDb_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnDb.setSelection(false);
btnDb_1.setSelection(false);
btnDb_2.setSelection(true);
btnDb_3.setSelection(false);
btnDb_4.setSelection(false);
resetLabels.doReset();
}
});
btnDb_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnDb.setSelection(false);
btnDb_1.setSelection(false);
btnDb_2.setSelection(false);
btnDb_3.setSelection(true);
btnDb_4.setSelection(false);
resetLabels.doReset();
}
});
btnDb_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
btnDb.setSelection(false);
btnDb_1.setSelection(false);
btnDb_2.setSelection(false);
btnDb_3.setSelection(false);
btnDb_4.setSelection(true);
resetLabels.doReset();
}
});
btnDb.setText("1 dB");
btnDb.setForeground(SWTResourceManager.getColor(255, 255, 255));
btnDb.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnDb.setAlignment(SWT.CENTER);
btnDb.setBounds(10, 18, 58, 30);
formToolkit.adapt(btnDb, true, true);
btnDb_1.setText("2 dB");
btnDb_1.setForeground(SWTResourceManager.getColor(255, 255, 255));
btnDb_1.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnDb_1.setAlignment(SWT.CENTER);
btnDb_1.setBounds(74, 18, 58, 30);
formToolkit.adapt(btnDb_1, true, true);
btnDb_2.setText("3 dB");
btnDb_2.setForeground(SWTResourceManager.getColor(255, 255, 255));
btnDb_2.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnDb_2.setAlignment(SWT.CENTER);
btnDb_2.setBounds(138, 18, 58, 30);
formToolkit.adapt(btnDb_2, true, true);
btnDb_3.setText("6 dB");
btnDb_3.setForeground(SWTResourceManager.getColor(255, 255, 255));
btnDb_3.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnDb_3.setAlignment(SWT.CENTER);
btnDb_3.setBounds(202, 18, 58, 30);
formToolkit.adapt(btnDb_3, true, true);
btnDb_4.setText("12 dB");
btnDb_4.setForeground(SWTResourceManager.getColor(255, 255, 255));
btnDb_4.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
btnDb_4.setAlignment(SWT.CENTER);
btnDb_4.setBounds(266, 18, 58, 30);
formToolkit.adapt(btnDb_4, true, true);
group_1.setTabList(new Control[]{btnDb, btnDb_1, btnDb_2, btnDb_3, btnDb_4});
//*****************************************************************************
//* Skylt som informerar om att Beräkand VO syns i fältet till höger
//*****************************************************************************
CLabel lblBerknadVo = new CLabel(shell, SWT.NONE);
lblBerknadVo.setEnabled(false);
lblBerknadVo.setTopMargin(8);
lblBerknadVo.setText("Ber\u00E4knad VO");
lblBerknadVo.setLeftMargin(15);
lblBerknadVo.setForeground(SWTResourceManager.getColor(255, 255, 255));
lblBerknadVo.setFont(SWTResourceManager.getFont("Sitka Display", 18, SWT.BOLD));
lblBerknadVo.setBottomMargin(0);
lblBerknadVo.setBackground(SWTResourceManager.getColor(153, 0, 0));
lblBerknadVo.setBounds(10, 434, 212, 59);
//*****************************************************************************
//* Skylt som informerar om beräknad VO efter tryck på kalkylknapp
//*****************************************************************************
lblYLabel.setText("VO");
lblYLabel.setAlignment(SWT.CENTER);
lblYLabel.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
lblYLabel.setBackground(SWTResourceManager.getColor(240,240,240));
lblYLabel.setBounds(233, 434, 100, 59);
formToolkit.paintBordersFor(lblYLabel);
//*****************************************************************************
//* Skylt som informerar om att VT ska anges
//*****************************************************************************
CLabel lblAlabel = MyFactory.createCLabel(shell, SWT.NONE);
lblAlabel.setEnabled(false);
lblAlabel.setText("Ange VT >= VO");
lblAlabel.setLeftMargin(15);
lblAlabel.setFont(SWTResourceManager.getFont("Sitka Display", 18, SWT.BOLD));
lblAlabel.setForeground(SWTResourceManager.getColor(255, 255, 255));
lblAlabel.setBackground(SWTResourceManager.getColor(153, 0, 0));
lblAlabel.setBounds(10, 499, 212, 59);
formToolkit.paintBordersFor(lblAlabel);
//*****************************************************************************
//* Skylt som visar inställd VT
//*****************************************************************************
VTvalueLabel.setBackground(SWTResourceManager.getColor(240,240,240));
VTvalueLabel.setAlignment(SWT.CENTER);
VTvalueLabel.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
VTvalueLabel.setText("VT");
VTvalueLabel.setBounds(467, 499, 100, 59);
formToolkit.paintBordersFor(VTvalueLabel);
//*****************************************************************************
//* Skala, potentiometer. Öka eller minska VT.
//*****************************************************************************
VTScale.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
VTScale.setForeground(SWTResourceManager.getColor(210, 105, 30));
VTScale.setPageIncrement(1);
VTScale.setMinimum(1);
VTScale.setMaximum(100);
VTScale.setSelection(50);
VTScale.setIncrement(1);
VTScale.setBackground(SWTResourceManager.getColor(255, 255, 255));
VTScale.setLocation(233, 505);
VTScale.setSize(228, 42);
// Beräkningar utförs då man drar i skjutpotentiometern.
// VT presenteras samt beräknade värden till A, B och C
VTScale.addSelectionListener(new SelectionListener(){
@Override
public void widgetSelected(SelectionEvent e) {
int scaleValue = VTScale.getSelection() + VTScale.getMinimum();
double u = scaleValue+97;
double q = u/99-1;
T.setTNet_VT(T.getTNet_VO()+(1-T.getTNet_VO())*q);
T.setTNet_VT((double) Math.round(T.getTNet_VT()*1000)/1000);
VTvalueLabel.setText(T.getTNet_VT()+" V");
double a = T.getTNet_Impedance()*(1-T.getTNet_VT());
T.setTNet_A(a);
T.setTNet_A((double) Math.round(T.getTNet_A()*10)/10);
double b = (T.getTNet_Impedance()*T.getTNet_VT())/(1-Math.pow(10, -(T.getTNet_Decibel()/20)));
T.setTNet_B(b);
T.setTNet_B((double) Math.round(T.getTNet_B()*10)/10);
double c = T.getTNet_Impedance()*(T.getTNet_VT()/(Math.pow(10,-(T.getTNet_Decibel()/20)))-1);
T.setTNet_C(c);
T.setTNet_C((double) Math.round(T.getTNet_C()*10)/10);
lblA.setText(""+T.getTNet_A());
lblB.setText(""+T.getTNet_B());
lblC.setText(""+T.getTNet_C());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
//*****************************************************************************
//* Displayer som presenterar beräknade resistansvärden
//*****************************************************************************
lblA.setText("A");
lblA.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
lblA.setAlignment(SWT.CENTER);
lblA.setBounds(10, 605, 100, 30);
formToolkit.paintBordersFor(lblA);
lblB.setText("B");
lblB.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
lblB.setAlignment(SWT.CENTER);
lblB.setBounds(116, 605, 100, 30);
formToolkit.paintBordersFor(lblB);
lblC.setText("C");
lblC.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
lblC.setAlignment(SWT.CENTER);
lblC.setBounds(222, 605, 100, 30);
formToolkit.paintBordersFor(lblC);
//*****************************************************************************
//* Skyltar som visar vilken display som är A, B eller C
//*****************************************************************************
CLabel label = MyFactory.createCLabel(shell, 0);
label.setForeground(SWTResourceManager.getColor(255, 245, 238));
label.setBackground(SWTResourceManager.getColor(105, 105, 105));
label.setEnabled(false);
label.setText("A");
label.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
label.setAlignment(SWT.CENTER);
label.setBounds(10, 641, 100, 30);
formToolkit.paintBordersFor(label);
CLabel label_1 = MyFactory.createCLabel(shell, 0);
label_1.setForeground(SWTResourceManager.getColor(255, 245, 238));
label_1.setBackground(SWTResourceManager.getColor(105, 105, 105));
label_1.setEnabled(false);
label_1.setText("B");
label_1.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
label_1.setAlignment(SWT.CENTER);
label_1.setBounds(116, 641, 100, 30);
formToolkit.paintBordersFor(label_1);
CLabel label_2 = MyFactory.createCLabel(shell, 0);
label_2.setForeground(SWTResourceManager.getColor(255, 245, 238));
label_2.setBackground(SWTResourceManager.getColor(105, 105, 105));
label_2.setEnabled(false);
label_2.setText("C");
label_2.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
label_2.setAlignment(SWT.CENTER);
label_2.setBounds(222, 641, 100, 30);
formToolkit.paintBordersFor(label_2);
//*****************************************************************************
//* Skylt-rubrik över displayer
//*****************************************************************************
CLabel lblResistorvrden = MyFactory.createCLabel(shell, 0);
lblResistorvrden.setText("Resistanser F\u00F6r \u00D6nskad D\u00E4mpning");
lblResistorvrden.setForeground(SWTResourceManager.getColor(255, 245, 238));
lblResistorvrden.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.BOLD));
lblResistorvrden.setEnabled(false);
lblResistorvrden.setBackground(SWTResourceManager.getColor(105, 105, 105));
lblResistorvrden.setAlignment(SWT.CENTER);
lblResistorvrden.setBounds(10, 564, 312, 30);
formToolkit.paintBordersFor(lblResistorvrden);
CLabel lblktrb = MyFactory.createCLabel(shell, 0);
lblktrb.setAlignment(SWT.CENTER);
lblktrb.setText("4kTRB");
lblktrb.setLeftMargin(15);
lblktrb.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
lblktrb.setFont(SWTResourceManager.getFont("Arial Black", 12, SWT.BOLD | SWT.ITALIC));
lblktrb.setEnabled(false);
lblktrb.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
lblktrb.setBounds(502, 605, 92, 30);
formToolkit.paintBordersFor(lblktrb);
CLabel lblRadioelectronicsoftware = MyFactory.createCLabel(shell, 0);
lblRadioelectronicsoftware.setAlignment(SWT.CENTER);
lblRadioelectronicsoftware.setText("Radio.Electronic.Software");
lblRadioelectronicsoftware.setLeftMargin(15);
lblRadioelectronicsoftware.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
lblRadioelectronicsoftware.setFont(SWTResourceManager.getFont("Arial Black", 12, SWT.BOLD | SWT.ITALIC));
lblRadioelectronicsoftware.setEnabled(false);
lblRadioelectronicsoftware.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
lblRadioelectronicsoftware.setBounds(328, 635, 266, 36);
formToolkit.paintBordersFor(lblRadioelectronicsoftware);
//********************************************************************************
//* Knappen Calculate. Lyssnare för beräkning av Vo.
//********************************************************************************
btnCalc.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
if (btnOhm.getSelection()) {
T.setTNet_Impedance(50);
} else if (btnOhm_1.getSelection()) {
T.setTNet_Impedance(75);
} else if (btnOhm_2.getSelection()) {
T.setTNet_Impedance(600);
}
if (btnDb.getSelection()) {
T.setTNet_Decibel(1);
} else if (btnDb_1.getSelection()) {
T.setTNet_Decibel(2);
} else if (btnDb_2.getSelection()) {
T.setTNet_Decibel(3);
} else if (btnDb_3.getSelection()) {
T.setTNet_Decibel(6);
} else if (btnDb_4.getSelection()) {
T.setTNet_Decibel(12);
}
T.setTNet_VO(Math.pow(10, -(T.getTNet_Decibel())/20)); // beräkna VO utifrån vald dB
T.setTNet_VO((double) Math.round(T.getTNet_VO()*1000)/1000); //VO med 3 decimaler
lblYLabel.setText(T.getTNet_VO() + " V");
VTScale.setEnabled(true);
VTScale.setFocus();
lblA.setText("A");
lblB.setText("B");
lblC.setText("C");
btnCalc.setEnabled(false);
}
});
}
}