0
我有一些鍵盤輸入的麻煩。我寫了一個KeyActionListener
,但它只在用鼠標點擊某個按鈕之後才起作用,例如,如果我用鼠標5按下,然後按下鍵盤,我會用鍵盤按下符號,但如果在程序啓動後按鍵盤上的鍵我什麼也沒得到,請幫助我。如何在java中輸入由keybord壓入標籤的符號?
這裏是我的代碼:
package calc;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;
public class View extends ViewPart {
public Text inputText;
public Text inputText2;
public Label label;
public Label label2;
public Calculation calcul;
public View()
{
calcul = new Calculation();
}
public void createPartControl(Composite parent) {
Composite mainComposite = new Composite(parent, SWT.NONE);
GridLayout gltop = new GridLayout(1, false);
gltop.numColumns = 1;
mainComposite.setLayout(gltop);
//mainComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
//ВЫзов верхнего и нижнего композита
createTop(mainComposite);
createBott(mainComposite);
}
private void createTop(Composite parent)
{
GridLayout gltop = new GridLayout(1, false);
gltop.numColumns = 1;
GridData data = new GridData(GridData.FILL_HORIZONTAL);
Composite topComposite = new Composite(parent, SWT.NONE);
topComposite.setLayout(gltop);
topComposite.setLayoutData(data);
//topComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
/*inputText = new Text(topComposite, SWT.BORDER);
inputText.setLayoutData(data);
inputText.setFont(SWTResourceManager.getFont("", 18, SWT.BOLD));*/
label = new Label(topComposite,SWT.RIGHT | SWT.BORDER);
label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
label.setLayoutData(data);
label.setFont(SWTResourceManager.getFont("", 18, SWT.BOLD));
label.setText(calcul.output);
}
private void createBott(Composite parent)
{
GridLayout glbot = new GridLayout(1, false);
glbot.numColumns = 2;
GridData data = new GridData(GridData.FILL_BOTH);
Composite botComposite = new Composite(parent, SWT.NONE);
botComposite.setLayout(glbot);
botComposite.setLayoutData(data);
//botComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
createLeft(botComposite);
createRight(botComposite);
}
private void createLeft(Composite parent)
{
Composite leftComposite = new Composite(parent, SWT.NONE);
GridLayout glleft = new GridLayout(1, false);
GridData data = new GridData(GridData.FILL_BOTH);
//glleft.marginTop = 26;
leftComposite.setLayout(glleft);
leftComposite.setLayoutData(data);
//leftComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
label2 = new Label(leftComposite,SWT.BORDER);
label2.setLayoutData(data);
Button buttonMC = createFuncDigButtons(leftComposite, data, "M+", 'P');
Button buttonMR = createFuncDigButtons(leftComposite, data, "M-", 'M');
Button buttonMS = createFuncDigButtons(leftComposite, data, "MR", 'R');
Button buttonMpl = createFuncDigButtons(leftComposite, data, "MC", 'c');
}
private void createRight(Composite parent)
{
GridLayout glright = new GridLayout(1, false);
GridData data = new GridData(GridData.FILL_BOTH);
Composite rightComposite = new Composite(parent, SWT.NONE);
glright.numColumns = 1;
rightComposite.setLayout(glright);
rightComposite.setLayoutData(data);
//rightComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
createRightTop(rightComposite);
createRightBot(rightComposite);
}
private void createRightTop(Composite parent)
{
Composite rightTopComposite = new Composite(parent, SWT.NONE);
GridLayout glltop = new GridLayout(1, false);
GridData data = new GridData(GridData.FILL_BOTH);
glltop.numColumns = 3;
glltop.marginBottom = 0;
glltop.marginTop = 0;
rightTopComposite.setLayout(glltop);
rightTopComposite.setLayoutData(data);
//rightTopComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
Button buttonBack = createFuncDigButtons(rightTopComposite, data, "Backspace", 'B');
Button buttonC = createFuncDigButtons(rightTopComposite, data, "C", 'C');
Button buttonCE = createFuncDigButtons(rightTopComposite, data, "CE", 'E');
}
private void createRightBot(Composite parent)
{
GridLayout glright = new GridLayout(1, false);
GridData data = new GridData(GridData.FILL_BOTH);
Composite rightBotComposite = new Composite(parent, SWT.NONE);
glright.numColumns = 5;
rightBotComposite.setLayout(glright);
rightBotComposite.setLayoutData(data);
//rightBotComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
Button button7 = createFuncDigButtons(rightBotComposite, data, "7", '7');
Button button8 = createFuncDigButtons(rightBotComposite, data, "8", '8');
Button button9 = createFuncDigButtons(rightBotComposite, data, "9", '9');
Button buttonDev = createFuncDigButtons(rightBotComposite, data, "/", '/');
Button buttonSQRT = createFuncDigButtons(rightBotComposite, data, "sqrt", 't');
Button button4 = createFuncDigButtons(rightBotComposite, data, "4", '4');
Button button5 = createFuncDigButtons(rightBotComposite, data, "5", '5');
Button button6 = createFuncDigButtons(rightBotComposite, data, "6", '6');
Button buttonMult = createFuncDigButtons(rightBotComposite, data, "*", '*');
Button buttonPer = createFuncDigButtons(rightBotComposite, data, "%", '%');
Button button1 = createFuncDigButtons(rightBotComposite, data, "1", '1');
Button button2 = createFuncDigButtons(rightBotComposite, data, "2", '2');
Button button3 = createFuncDigButtons(rightBotComposite, data, "3", '3');
Button buttonMinus = createFuncDigButtons(rightBotComposite, data, "-", '-');
Button buttonDev1 = createFuncDigButtons(rightBotComposite, data, "1/x", 'X');
Button button0 = createFuncDigButtons(rightBotComposite, data, "0", '0');
Button buttonPM = createFuncDigButtons(rightBotComposite, data, "+/-", 'p');
Button buttonD = createFuncDigButtons(rightBotComposite, data, ".", '.');
Button buttonPlus = createFuncDigButtons(rightBotComposite, data, "+", '+');
Button buttonR = createFuncDigButtons(rightBotComposite, data, "=", '=');
}
public Button createFuncDigButtons(Composite parent, GridData gridData, final String digit, final char formethod)
{
Button button = new Button(parent, SWT.PUSH);
button.setLayoutData(gridData);
button.setText(String.valueOf(digit));
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
// update(formethod);
calcul.getDigit(formethod);
label.setText(calcul.output);
if (calcul.memtrue)
{
label2.setText("M");
}
else
{
label2.setText("");
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
/* do nothing */
}
});
button.addKeyListener(new KeyListener() {
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyPressed(KeyEvent arg0) {
calcul.getDigit(arg0.character);
label.setText(calcul.output);
}
});
return button;
}
public void setFocus() {
label.setFocus();
}
}
此外,我怎麼能佈局我的左右按鍵的話,他們將是相同的大小?我的操作可以得到很好的結果,我仍然有另一個大小的記憶按鈕,然後右按鈕。以下是圖像:
*「它的工作原理後,才通過鼠標點擊按鈕的一些」 *嘗試跳格幾次。重點可能放在GUI頂部的那個'tab'上。 –
安德魯湯普森,謝謝,是的,我知道這件事,但我怎麼能沒有絆倒呢? – DPS
moreovew如何左右佈局,使它們的寬度相同? – DPS